Understanding and Handling Terminating Errors

by Oct 13, 2011

To suppress errors in a cmdlet, you can use the common parameter -ErrorAction SilentlyContinue. Here is a sample:

Get-WmiObject Win32_BIOS -ComputerName localhost, offline, 127.0.0.1 -ErrorAction SilentlyContinue | Select-Object __Server, Manufacturer, SerialNumber

This will get you a neat list of BIOS versions used by the computers in your list. Machines that are offline will simply be ignored thanks to the ErrorAction parameter.

However, you cannot handle "terminating" errors that way. Terminating errors are errors that the cmdlet developer thought were especially serious. Get-WmiObject throws terminating errors when you have not sufficient privileges while accessing a remote system (not your own):

PS> Get-WmiObject Win32_BIOS -ComputerName storage1 -Credential dummy -ErrorAction SilentlyContinue

Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

To handle this kind of error, you need an error handler:

PS> Get-WmiObject Win32_BIOS -ComputerName storage1 -Credential dummy -ErrorAction SilentlyContinue

Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
PS> try {Get-WmiObject Win32_BIOS -ComputerName storage1 -Credential dummy -ErrorAction SilentlyContinue } catch { Write-Warning "Oops: $_" }

Note that in this example, benign non-terminating errors are suppressed by the ErrorAction parameter whereas more "serious" terminating errors are handled by the catch block.

Twitter This Tip!
ReTweet this Tip!