2

I have a script but it takes hours to execute. What do i need to do to make this run in parallel?

 $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
 }
2
  • In parallel with what? Sep 14, 2011 at 11:41
  • "Myserverlist.txt" contains a list of servers that I want checked. Powershell runs the command on each server in order they are listed in the text file. I was wondering what can be done in order for all the servers to have that command run at once instead of one-at-a-time.
    – Jake
    Sep 14, 2011 at 11:44

1 Answer 1

5

Referencing this Stack Overflow post.

Running your construct on 3 servers of mine took 2:23.

Running the below script took 2:07. Not a lot of savings, but running on a larger number may get you more time savings. You can also mess around with how you output at the end I think.

# 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .