我有一个脚本,但需要数小时才能执行。 我需要做什么才能使其平行运行?
$servers = Get-Content -Path c:\Scripts\MyServerList.txt foreach ($Server in $servers) { Write-Output $Server; Get-EventLog -LogName System -EntryType Error -ComputerName $Server | Measure-Object }
引用这个堆栈溢出文章。
在我的3台服务器上运行你的构build花了2:23。
运行下面的脚本花了2:07。 没有太多的节省,但运行在更大的数量可能会让您节省更多的时间。 你也可以混淆你最终的输出结果。
# Loop through the server list Get-Content "C:\scripts\Servers.txt"| %{ # Define what each job does $ScriptBlock = { param($Server) Write-Output $Server; Get-EventLog -LogName System -EntryType Error -ComputerName $Server | Measure-Object|Out-String -Stream } # Execute the jobs in parallel Start-Job $ScriptBlock -ArgumentList $_ } # Wait for it all to complete While (Get-Job -State "Running") { Start-Sleep 1 } # Getting the information back from the jobs Get-Job | Receive-Job|Write-Host