Start services in parallel

ⅰ亾dé卋堺 提交于 2019-12-02 08:33:36

Here is an example of parallel processing using Powershell and Workflows:

$server_list = Get-Content -path D:\Path\list_of_servers.txt
workflow MyWorkflow
{
    foreach -parallel($s in $server_list) { 
        inlinescript { (Get-Service -Name '*Service Name*' -PSComputerName $s) | Where-Object {$_.status -eq "Stopped"} | Set-Service -Status Running
        }
    }
}

Using Powershell V2 and jobs

Untested code, but should be close:

$server_list = Get-Content -path D:\Path\list_of_servers.txt

$maxJobs = 5
$scriptblock = { 
(Get-Service -Name $args[1] -ComputerName $args[0]) | Where-Object {$_.status -eq "Stopped"} | Set-Service -Status Running
}

foreach ($s in $server_list)
{
    $arguments = @($s, $service)

    $running = @(Get-Job | Where-Object { $_.State -eq 'Running' })
    while ($running.Count -gt ($maxJobs -1)) {
        $done = Get-Job | Wait-Job -Any
        $running = @(Get-Job | ? {$_.State -eq 'Running'})
    }   
    start-job -scriptblock $scriptblock -ArgumentList $arguments
}
Get-Job | Wait-Job
Get-Job | Receive-Job
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!