PowerShell多任务

代码

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
}

主要命令

Start-Job

Wait-Job

Get-Job

Receive-Job

注意

每一个Job至少包含一个子Job,参考:https://devblogs.microsoft.com/scripting/powershell-jobs-week-introduction-to-powershell-jobs/

原文地址:https://www.cnblogs.com/talentzemin/p/13099312.html