Run N parallel jobs in powershell

后端 未结 6 1523
忘了有多久
忘了有多久 2020-12-07 23:18

I have the following powershell script

$list = invoke-sqlcmd \'exec getOneMillionRows\' -Server...
$list | % {
    GetData $_ > $_.txt
    ZipTheFile $_.t         


        
6条回答
  •  有刺的猬
    2020-12-07 23:43

    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.

    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
    }
    

    Hope this helps.

提交回复
热议问题