Run N parallel jobs in powershell

后端 未结 6 1542
忘了有多久
忘了有多久 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:55

    Old thread but I think this could help:

    $List = C:\List.txt
    $Jobs = 8
    
    Foreach ($PC in Get-Content $List)
    {
    Do
        {
        $Job = (Get-Job -State Running | measure).count
        } Until ($Job -le $Jobs)
    
    Start-Job -Name $PC -ScriptBlock { "Your command here $Using:PC" }
    Get-Job -State Completed | Remove-Job
    }
    
    Wait-Job -State Running
    Get-Job -State Completed | Remove-Job
    Get-Job
    

    The "Do" loop pause the "foreach" when the amount of job "running" exceed the amount of "$jobs" that is allowed to run. Than wait for the remaining to complete and show failed jobs...

提交回复
热议问题