New-TaskPool.ps1 - Threading the Powershell way Pt 2
I’ve made a few modifications to New-TaskPool. My original script couldn’t easily be loaded into your profile. You had to specify the script file ( .\New-TaskPool.ps1 ). This quickly became a problem as I would invariably be in the wrong folder and typing out the full path tested my patience. With that in mind, I made modifications that allowed me to encapsulate New-TaskPool into a function that could be loaded from my profile. Overall the usage is very similar. You'll find the full script attached to this post.
You may also notice that I’ve started using a collection of a custom object instead of 5 or 6 separate collections for different variables. So:
$script:rscfg = @(); (0..($pools-1)) | % { $rscfg += 0 }
$script:rs = @(); (0..($pools-1)) | % { $rs += 0 }
$script:pipe = @(); (0..($pools-1)) | % { $pipe += 0 }
$script:pipetimer = @(); (0..($pools-1)) | % { $pipetimer += 0 }
$script:cache = @(); (0..($pools-1)) | % { $cache += 0 }
Has become:
$pipemgr = @(); (0..($pools-1)) | % { $pipemgr += 0 | select-object rscfg,rs,pipe,pipetimer,input,error,output }
In addition, I’ve increased performance overall. Instead of recreating the runspace and pipeline for each job, I’m reusing the runsapce and just creating a new pipeline. In one of my speed comparisons, I had 300 items and a “dummy” script block that did nothing but write-output the input. For those 300 items under my original New-TaskPool, it took 31 seconds. With the modifications show in this article, it’s down to 1.5 seconds. Whoo! This is still slightly experimental, so I would like to hear if anyone has a problem with the way this is working now.
Since New-TaskPool is now a function, you can either load it via your profile or just dot-source it in your console when needed. Here’s an example of it’s usage:
215# # Example scriptblock
216# $block = {
>> $input | % {
>> $fixed = gwmi -co $_ -class win32_quickfixengineering -ea silentlycontinue | ? { $_.HotFixID -eq "KB931836" }
>> write-Output $fixed
>> }
>> }
>>
217# # Dot-Source function into your current session:
218# . .\New-TaskPool.ps1
219#
219# "testmachine","testmachine","testmachine","testmachine" | new-taskpool -scriptblock $block
Description : Update for Windows Server 2003 (KB931836)
FixComments : Update
HotFixID : KB931836
Install Date :
InstalledBy : SYSTEM
InstalledOn : 2/14/2007
Name :
ServicePackInEffect : SP3
Status :
Description : Update for Windows Server 2003 (KB931836)
FixComments : Update
HotFixID : KB931836
Install Date :
InstalledBy : SYSTEM
InstalledOn : 2/14/2007
Name :
ServicePackInEffect : SP3
Status :
Description : Update for Windows Server 2003 (KB931836)
FixComments : Update
HotFixID : KB931836
Install Date :
InstalledBy : SYSTEM
InstalledOn : 2/14/2007
Name :
ServicePackInEffect : SP3
Status :
Description : Update for Windows Server 2003 (KB931836)
FixComments : Update
HotFixID : KB931836
Install Date :
InstalledBy : SYSTEM
InstalledOn : 2/14/2007
Name :
ServicePackInEffect : SP3
Status :
Tomorrow, I will be posting an example scriptblock that I use on a daily basis to scan the network for AntiVirus compliance.
Gaurhoth