Created
January 29, 2015 17:39
-
-
Save vquaiato/d63dc1279e763458b1ad to your computer and use it in GitHub Desktop.
using background jobs in powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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