I want to run a background job in my PowerShell-Script to read all file icons from a specific folder into a [System.Windows.Forms.ImageList].
$JobScript = {
param([String] $Path)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Files = [System.IO.DirectoryInfo]::new($Path).GetFiles()
[System.Windows.Forms.ImageList] $ImgList = [System.Windows.Forms.ImageList]::new()
$Files.ForEach({
$Extension = [System.IO.FileInfo]::new($_.FullName).Extension
if ($Extension -notin $ImgList.Images.Keys)
{
$ImgList.Images.Add($Extension, [System.Drawing.Icon]::ExtractAssociatedIcon($_.FullName))
}
})
$ImgList
}
$Job = Start-Job -ScriptBlock $JobScript -ArgumentList "C:\Windows"
#Do other stuff
Wait-Job $Job
Receive-Job $Job
The code generates plenty of errors, stating that neither [System.Drawing.Icon] nor [System.Windows.Forms.ImageList] can be found. When I run the code without a job it works just fine. Strangely the System.IO-namespace is working alright.
I guess I need to use add-type inside the script block, but how do I add these system types with it?
EDIT: I updated the code with the help of Mathias' comment.