This function will take in a script or scriptblock, and run it against specified objects(s) in parallel. It uses runspaces, as there are many situations where jobs or PSRemoting are not appropriate.
#Download and unblock the file(s).
#Dot source the file.
. "\\Path\To\Invoke-Parallel.ps1"
#Get help for the function
Get-Help Invoke-Parallel -Full
#Use Invoke-Parallel with variables in your session
$Number = 2
1..10 | Invoke-Parallel -ImportVariables -ScriptBlock { $Number * $_ }
Some outdated notes and details are available on the TechNet Galleries submission.
Would love contributors, suggestions, feedback, and other help! Split this out at the suggestion of @vors to help enable collaboration.
- Credit to Boe Prox for the base code this uses. Boe has a number of references on runspaces, including the presentation and materials here and the excellent PoshRSJob module; check them out!