source: https://stackoverflow.com/questions/8781666/run-n-parallel-jobs-in-powershell
The Start-Job cmdlet allows you to run code in the background. To do what you’d ask, something like the code below should work. Use Wait-Job -Any to emulate throttle.
foreach ($server in $servers) {
$running = @(Get-Job | Where-Object { $_.State -eq 'Running' })
if ($running.Count -le 8) {
Start-Job {
Add-PSSnapin SQL
$list = invoke-sqlcmd 'exec getOneMillionRows' -Server...
...
}
} else {
$running | Wait-Job
}
Get-Job | Receive-Job
}