Microsoft – PowerShell – Error Handling

Microsoft – PowerShell – Error Handling

Still struggling along with PowerShell and wishing someone will say “Walk a little Straighter” (http://www.youtube.com/watch?v=U1no7Or9BeI) to me.

But, from reading through others lens, here is my current understanding.

At a higher level, for instance at a module level, one gets to choose how errors are handled.

To do so, one needs to set the system variable $ErrorActionPreference.

The various available options for $ErrorActionPreference:

a) SilentlyContinue – Errors are not displayed and Script Continues Execution without   visually acknowledging an error has occurred

b) Continue – Errors are displayed and Script continues execution

c) Stop – Errors are displayed and the Script is terminated

d) Inquire – Errors are displayed and user is prompted\asked how he \ she will like to proceed

To establish your choice, use something resembling:

$ErrorActionPreference = "SilentlyContinue"

Note that PowerShell also supports statement level “Error Reporting”.

My first initiation into this construct was from Steve Murawski’s Blog Entry.

Soccer is called the “Beautiful Game” and some words are best left hanging alone:

Deep Dive Error Handling:

http://blog.usepowershell.com/category/deep-dive/error-handling/So what happens when you encounter an error?

If you hit a terminating error, your script, function, or command will stop.  The error will be logged to the $error variable and written to the error stream.  The $? will be set to false.  You can use the trap construct (or the try/catch/finally construct in V2) to deal with terminating errors and we’ll cover that further in the fourth post in this series.

If you hit a non-terminating error, the result will depend on the $ErrorActionPreference in the scope or the –ErrorAction parameter.  If the $ErrorActionPreference or –ErrorAction parameter is set to ‘Continue’, the error will be written to the error stream, added to the $error array, and the $? will be set to $false.  ‘SilentlyContinue’ will not write an error to the Error stream, but $error and $? are updated.  Finally, ‘Inquire’ provides you an option at each error as to whether to treat it as terminating or non-terminating.  If treated as a non-terminating error, the error will be treated like the $ErrorActionPreference or –ErrorAction parameter is ‘Continue’.

From what I can surmise:

When an error is encountered, the following things happen:

  1. The System Array variable ($error) is updated; in this case a new entry is added to it
  2. A System Boolean variable ($?) is updated with the system keyword $false
  3. And, depending on the global variable $ErrorActionPreference or the statement level argument -ErrorAction, the error will be displayed or “eating up”
  4. At the statement level, errors can also be saved by using the “ErrorVariable” parameter

References:

  1. Use PowerShell – Error Handling (Deep Dive) http://blog.usepowershell.com/category/deep-dive/error-handling/
  2. Hey, Scripting Guy! How Can I Use $ErrorActionPreference to Control Cmdlet Handling of Errors?
    http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/08/hey-scripting-guy-march-8-2010.aspx
  3. Write Warning
    http://ss64.com/ps/write-warning.html

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s