Last active
July 5, 2019 20:28
-
-
Save countzero/ef6c801456146664cf9da120cfec56cd to your computer and use it in GitHub Desktop.
Execute PowerShell jobs in parallel.
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
for ($index=1; $index -le 23; $index++) { | |
$scriptBlock = { | |
Param ( | |
[string] [Parameter(Mandatory=$true)] $id | |
) | |
$durationInMilliseconds = $(Get-Random -Minimum 500 -Maximum 1000) | |
Start-Sleep -Milliseconds $durationInMilliseconds | |
Write-Host "Job #${id} completed in ${durationInMilliseconds} ms." | |
} | |
Write-Host "Starting Job #${index}..." | |
Start-Job $scriptBlock -Name "Job #${index}" -ArgumentList $index | Out-Null | |
} | |
Get-Job | Receive-job -AutoRemoveJob -Wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will output the job results after all jobs have been completed in the order in which they have finished: