67

I am trying to see if a process is running on multiple servers and then format it into a table.

get-process -ComputerName server1,server2,server3 -name explorer | Select-Object processname,machinename

Thats the easy part - When the process does not exist or if the server is unavailable, powershell outputs a big ugly error, messes up the the table and doesn't continue. Example

Get-Process : Couldn't connect to remote machine.At line:1 char:12 + get-process <<<<  -ComputerName server1,server2,server3 -name explorer | format-table processname,machinename
+ CategoryInfo          : NotSpecified: (:) [Get-Process], InvalidOperatio   nException    + FullyQualifiedErrorId : System.InvalidOperationException,Microsoft.Power   Shell.Commands.GetProcessCommand

How do I get around this? If the I would still like to get notified if the process isn't available or Running.

roeme
  • 3,955
  • 3
  • 23
  • 34
Jake
  • 2,388

3 Answers3

77

Add -ErrorAction SilentlyContinue to your command.

When it's not an error, but an unhandled Exception, you should add -EV Err -EA SilentlyContinue, in order to catch the exception. (EA is an alias for ErrorAction)

You can then evaluate the error in your script, by having a look at $Err[0]

evandrix
  • 117
Bart De Vos
  • 18,171
26

Short answer: Add $ErrorActionPreference = 'SilentlyContinue' at the start of your code so you don't need to add -ErrorAction SilentlyContinue to every command

Long answer: Controlling Error Reporting Behavior and Intercepting Errors

Moby Disk
  • 196
Tinman
  • 359
0

Try piping the server names one at a time to the command instead. Then you can error handle one machine at a time.

'server1', 'server2', 'server3' |
  foreach {
    try {
      Get-Process -ComputerName $_ -Name Explorer -ErrorAction SilentlyContinue
    } catch {} #Yes. I actually don't care about any errors.
  } |
    select ProcessName, MachineName

This, of course, won't catch any errors depending on network issues or access permissions. So we can't tell for sure that you've actually got all machines in the list with Explorer running.

Note: Get-Process with parameter -ComputerName is no longer available in PowerShell Core. That would require the usage of Invoke-Command instead.

Dennis
  • 101