start-job Run command in parallel and output result as they arrive

23,102

With Powershell V2 you can use jobs as in @Andy answer or also in further detail in this link Can Powershell Run Commands in Parallel?

With PowerShell V2 you may also want to check out this script http://gallery.technet.microsoft.com/scriptcenter/Foreach-Parallel-Parallel-a8f3d22b using runspaces

With PowerShell V3 you have the foreach -parallel option.

for example (NB Measure-Command is just there for timing so you could make a comparison)

Workflow Test-My-WF {  
  param([string[]]$servers)

  foreach -parallel ($server in $servers) {

    $isPatched = (Get-HotFix -ComputerName $server | where {$_.HotFixID -eq 'KB9s82018'}) -ne $null     
    If ($isPatched) 
    { 
        $server | Out-File -FilePath "c:\temp\_patchlist.txt" -Append
    } 
    Else  
    { 
        $server | Out-File -FilePath "c:\temp\_output.txt" -Append 
    } 
  }
}

Measure-Command -Expression { Test-My-WF   $servers }
Share:
23,102
Fenomatik
Author by

Fenomatik

Updated on March 29, 2020

Comments

  • Fenomatik
    Fenomatik over 3 years

    I am trying to get specific KBXXXXXX existence on a list of servers , but once my script one server it takes time and return result and come back and then move to next one . this script works perfectly fine for me . I want my script to kick off and get-hotfix as job and other process just to collect the results and display them.

    $servers = gc .\list.txt 
    foreach ($server in $servers) 
    { 
        $isPatched = (Get-HotFix -ComputerName $server | where HotFixID -eq 'KBxxxxxxx') -ne $null 
        If ($isPatched) 
        { 
        write-host $server + "Exist">> .\patchlist.txt} 
        Else  
        { 
        Write-host $server +"Missing"
    $server  >> C:\output.txt
        } 
    
    }
    

    The objective it to make the list execute faster rather than running serially.

  • riv_rec
    riv_rec over 10 years
    @alroc thats interesting, could you link to some online articles on this type of parallel processing. Thanks
  • alroc
    alroc over 10 years
    @PaulRowland, unfortunately I am having a hard time finding good documentation on "how" it works. Have a look at the help for Get-Hotfix and the first few hits here. Jobs are good when you need them, but they can introduce unnecessary complexity. Use what PowerShell provides you - MS has probably done the parallelism better than you would.
  • riv_rec
    riv_rec over 10 years
    @alroc - thanks for that but I didn't see where it suggests its doing them in parallel. It would also be interesting to know if its the same under the covers as using the foreach -parallel option. I don't have test servers to hand right now but I may run a script to compare the two.
  • Fenomatik
    Fenomatik over 10 years
    The above code didnt do any good to speed up the process by executing the series of same command to different servers in parallel .
  • alroc
    alroc over 10 years
    I've been corrected via Twitter; invoke-command runs parallel, but other cmdlets are sequential (see the "Retrieving WMI Information from Multiple Computers" section here). You can use the technique outlined there to use Invoke-Commandto get the parallelism without resorting to jobs.