Kevin DeVoe
Kevin DeVoe

Reputation: 602

Powershell. Running tasks in parallel. Waiting for all

Powershell is not my forte so I have a question which is probably straight forward but am unable to find a clear answer.

As part of another process I have the need to stop[do work]start services across several servers. I have this working but was looking to make it more efficient because the start can take 5-10 minutes to complete. Right now it's running serialized so starting 8 services takes 40+ minutes. What's the best way of threading the start command and then checking to make sure all of them completed before moving to the next step?

Upvotes: 0

Views: 671

Answers (1)

Gzeh Niert
Gzeh Niert

Reputation: 168

You could wrap the service action in one job per target server and wait for all job to be complete.

To create the jobs: foreach ($Server in $Servers) { Start-Job { param($ServiceName); Stop-Service $ServiceName } -Name $Server -ArgumentList $ServiceName } or switch to CmdLet Start-Service for starting the service.

Cf. https://technet.microsoft.com/en-us/library/hh849698.aspx

To wait for jobs completion: Get-Job | Wait-Job

Cf. https://technet.microsoft.com/en-us/library/hh849693.aspx and https://technet.microsoft.com/en-us/library/hh849735.aspx

To get the result of jobs for the "go to next step" checks: Get-Job | Receive-Job

Cf. https://technet.microsoft.com/en-us/library/hh849718.aspx

Here you'll find an example of parallel execution using jobs that may help you: http://www.get-blog.com/?p=22

All these CmdLet are available and working (quite) the same since v2 as far as I know.

Upvotes: 1

Related Questions