0

I am trying to run a fairly simple command on 20 servers in parallel. What I have so far is this :

    $servers = @()
    $servers = (
         "server1",
         "server2",
         "server3",
         "server4",
         "server5",
         "server6"  
    )
    
    $jobs = foreach($server in $servers){
        try{
            $pw = convertto-securestring -AsPlainText -Force -String MyPassword
            $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "Domain\Myuser",$pw

            Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock { Get-WmiObject -Class Win32_Product | select Name, Version, Vendor | Sort-Object -Property Vendor | where Name -EQ "New Relic Infrastructure Agent" } -AsJob
        }
        catch{
            write-output "ERROR : Failed to connect to server $($server)"
            $_.Exception.Message
            $Error[0].Exception.PSObject.Properties | select Name,Value | FL *
        }    
}

get-job

While ($(Get-Job -State Running).count -gt 0){
    start-sleep 1
}

foreach($job in Get-Job){
    $results= Receive-Job -Id ($job.Id)
}

$results | select PSComputerName, Version | Sort-Object -Property Version | Format-Table -AutoSize

Get-job | Remove-Job -force

UPDATE: I have updated the code above to what I currently have. What is happening now is I am getting one result back for one server. the other jobs are just cancelled or dont finish running, I'm not sure.

I have incorporated the suggestions from the comments, but it looks like I am still not quite there. Any ideas?

evolmonster
  • 237
  • 1
  • 5
  • 16
  • Why use session(s) for this when `Invoke-Command` also has parameters `-ComputerName` and -`Credential`? Especially since you never seem to do `Disconnect-PSSession`... BTW, try not to use `+=` concatenation to an array. Use `$jobs = foreach($server in $servers){...}` instead and let PowerShell collect the results. – Theo Aug 03 '21 at 19:01
  • Does it still happen if you move server5 up the list? I don't think there is such a low limit on sessions. It seems to me that something is going wrong with `New-PSSession` when you get to the fifth server in the list. You will have to check the value of the variable before running `Invoke-Command`. When you receive your error, you could run `$Error[0].Exception.PSObject.Properties | select Name,Value | FL *` for more detail about what is going wrong (if there is an InnerException you can insert `InnerException.` after `Exception.` to see its details/stack trace and then `$Error[1]` etc. – Ash Aug 03 '21 at 19:03
  • That error grab is very helpful thank you! It showed me that it cannot find the service I am looking for on that box, so thats what the error was! And I have also removed the session creation, and that seems to be working in parallel now. I do get an error on one of the servers, which does appear to work if I log on locally and run the script, so not sure why that is failing: WinRM cannot complete the operation. Verify that the specified computer name is valid, that the computer is accessible over the network... – evolmonster Aug 03 '21 at 19:22
  • Sounds like you have some network connections to investigate. Ensure you can connect over 5985 from the machine running the code to the server. – Ash Aug 03 '21 at 19:57
  • I added the latest code above, incorporating all of the changes suggested. Still not working as expected! – evolmonster Aug 04 '21 at 19:28

0 Answers0