Skip to content

Instantly share code, notes, and snippets.

@vquaiato
Created January 29, 2015 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vquaiato/d63dc1279e763458b1ad to your computer and use it in GitHub Desktop.
Save vquaiato/d63dc1279e763458b1ad to your computer and use it in GitHub Desktop.
using background jobs in powershell
$jobItems = "a", "b", "c", "d", "e"
$jobMax = 2
$jobs = @()
$jobWork = {
param ($MyInput)
if ($MyInput -eq "d") {
throw "an example of an error"
} else {
write-output "Processed $MyInput"
}
}
foreach ($jobItem in $jobItems) {
if ($jobs.Count -le $jobMax) {
$jobs += Start-Job -ScriptBlock $jobWork -ArgumentList $jobItem
} else {
$jobs | Wait-Job -Any
}
}
$jobs | Wait-Job
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment