Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
 }
share|improve this question
    
In parallel with what? –  John Gardeniers Sep 14 '11 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 '11 at 11:44

1 Answer 1

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.