如何让这个脚本并行运行?

Jak*_*ake 2 powershell

我有一个脚本,但执行需要几个小时。我需要做什么才能使这个并行运行?

 $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
 }
Run Code Online (Sandbox Code Playgroud)

Chr*_*her 5

参考这篇Stack Overflow 帖子。

在我的 3 台服务器上运行您的构建需要 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
Run Code Online (Sandbox Code Playgroud)