Reputation: 43
i have script where i extract the icon so i can use it later in my script to import it into SCCM.
during the script i have to save the file as bmp. for that i use following code.
$Iitem = Get-ChildItem -Path "$PSScriptRoot" -Include "Icon.*" -File -Recurse
$objVar.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon("$Iitem");
$objVar.Icon.ToBitmap().Save("$path\Icon.bmp")
after that i take the bmp and save it as .jpg
$image = [drawing.image]::FromFile("$path\Icon.bmp")
now i want to Delete the .bmp File but i cant because the process is still in use... i can only delete it after i close PS
i tryed to do the bmp save part as a job
Invoke-Command $objApplication.Icon.ToBitmap().Save("$path\Icon fuer Anwendungskatalog.bmp") -AsJob
which creates the .bmp file even though i get an Error because of missing Session param... AND the file can't be deleted.
so the Question is how can i crate the file and delete it during runtime OR how can i save the icon directly as a jpg.
thanks in advance
best regards
Upvotes: 1
Views: 221
Reputation: 17064
$image
is System.Drawing.Image which implements IDisposable. And you should always dispose IDisposable.
In this case it will keep the lock on the file until you call Dispose().
In PowerShell, there is no using
statement, but you can use try + finally:
try {
$image = ...
}
finally {
if ($image) { $image.Dispose() }
}
Upvotes: 2