Advertisement
Guest User

Catching connection errors in invoke-command -asjob

a guest
Apr 23rd, 2017
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $message = $null
  2. $servers = "Server1","Server2","Server3","Server4"
  3.  
  4. $jobs =invoke-command -computername $servers -ScriptBlock{get-service} -asjob
  5. $jobs | wait-job
  6.  
  7. foreach($job in $jobs.ChildJobs)
  8. {
  9. try{ receive-job $job -keep -erroraction stop}
  10. catch [Management.Automation.Remoting.PSRemotingTransportException]{$message += "$((($_.Exception.ErrorRecord.Exception.message).split(" "))[4]) - Connection Failed`n"}
  11.  
  12. }
  13. write-host $message
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement