Below is the example:
$scriptBlock = { param( [string]$name, [string]$gender, [int]$seconds )
Start-Sleep -Seconds $seconds; Echo "$name `t`t $gender"
}
$jobsList = @()
$jobsList += Start-Job -ScriptBlock $scriptBlock -ArgumentList "Xueyi", "Male",7
$jobsList += Start-Job -ScriptBlock $scriptBlock -ArgumentList "ABC", "Male",4
$jobsList += Start-Job -ScriptBlock $scriptBlock -ArgumentList "DEF", "Male",11
$jobsList += Start-Job -ScriptBlock $scriptBlock -ArgumentList "EFF", "Male",9
$jobsList += Start-Job -ScriptBlock $scriptBlock -ArgumentList "PPPP", "Male",6
while ($true)
{
$allExit = $true;
foreach ($proc in $jobsList)
{
$allExit = $allExit -and ($proc.State -eq "Completed")
}
if ($allExit -eq $true)
{
$jobsList | % { Receive-Job $_ };
break;
}
else
{
Echo "Some processes still running, wait 1 second..."
}
Start-Sleep -seconds 1
}
Test output:
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Some processes still running, wait 1 second...
Xueyi Male
ABC Male
DEF Male
EFF Male
PPPP Male
<The End>