Reputation: 302
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?
Upvotes: 0
Views: 419