header image

Scripting games–ErrorActionPreference

Posted by: | May 12, 2013 | No Comment |

I’ve seen a lot of this type of thing in events 1 and 2

$ErrorPref = $ErrorActionPreference
$ErrorActionPreference = "Stop"

 

Don’t

The default for $ErrorActionPreference is Continue.  This means that the error message is shown and the cmdlet attempts to continue. The possible values (from about_Preference_Variables)

Stop:               Displays the error message and stops  executing.

Inquire:            Displays the error message and asks you  whether you want to continue.

Continue:           Displays the error message and continues executing.
(Default)          

SilentlyContinue:   No effect. The error message is not displayed and execution continues without interruption.

This preference variable only affects non-terminating errors. A terminating error will still stop processing.  Using

$ErrorActionPreference = "Stop"

effectively turns all errors in to terminating errors.

There are times when you want to stop processing and deal with the error such as

try {

some cmdlet

}

catch {

do something

}

In this case use –ErrorAction Stop  on the cmdlet to force errors to be terminating. Just makes sure you have the code in place to catch the error.

under: PowerShell original, Scripting Games