Handling Errors the PowerShell Way

Doctor Scripto

Summary: Trevor Sullivan talks about handling errors in Windows PowerShell.

Microsoft Scripting Guy, Ed Wilson, is here. Today we have guest blogger and Windows PowerShell MVP, Trevor Sullivan… also find Trevor on Twitter (https://twitter.com/pcgeek86) and his blog (http://trevorsullivan.net)

Microsoft Scripting Guy, Ed Wilson, just wrote a post about how to use the Try-Catch-Finally blocks in Windows PowerShell. But have you ever wondered if that was the only way to handle errors? It turns out that although it’s a great way to handle errors, there are still other options!

If you’re coming to Windows PowerShell from a software development background, you’ll most likely pick up on Try-Catch-Finally pretty easily. On the other hand, if you’re new to scripting, or you are a curious, knowledge-driven individual, you might want to consider what we’re talking about today.

Common parameters

When Windows PowerShell 2.0 came out, a new concept was introduced, called Advanced Functions. This concept allows you to develop commands that have the same feel as compiled cmdlets, while writing them in Windows PowerShell script syntax. One of the benefits of developing cmdlet-style commands instead of basic functions, is that they offer a few “common parameters.” Two of these common parameters are related to error handling: -ErrorAction and -ErrorVariable.

For more information about common parameters in advanced functions and compiled cmdlets, run this command at the Windows PowerShell prompt:

Get-Help -Name about_CommonParameters;

ErrorVariable Parameter

Normally, if you run a Windows PowerShell command and an error occurs, the error record will be appended to the “automatic variable” named $error. When you use the -ErrorVariable parameter in a call to a command, the error is assigned to the variable name that you specify. It’s important to note that even when you use the -ErrorVariable parameter, the $error variable is still updated.

By default, the -ErrorVariable parameter will overwrite the variable with the name that you specify. If you want to append an error to the variable, instead of overwriting it, you can put a plus sign (+) in front of the variable name. Let’s take a look at an example:

Stop-Process -Name invalidprocess -ErrorVariable ProcessError;
$ProcessError;
Stop-Process -Name invalidprocess2 -ErrorVariable +ProcessError;
if ($ProcessError) {
    ######## Take administrative action on error state
}

Image of command output

ErrorAction parameter

The -ErrorAction common parameter allows you to specify which action to take if a command fails. The available options are: Stop, Continue, SilentlyContinue, Ignore, or Inquire. If you’re developing a Windows PowerShell workflow, you can also use the Suspend value. However, advanced functions cannot be suspended.

When you specify the ErrorAction parameter during a call to a command, the specified behavior will override the $ErrorActionPreference variable in Windows PowerShell. This variable is part of a handful of variables known as “preference variables.” By default, Windows PowerShell uses an error action preference of Continue, which means that errors will be written out to the host, but the script will continue to execute. Hence, these types of errors are known as “non-terminating” errors.

If you set $ErrorActionPreference to Stop or if you use Stop as the parameter value for -ErrorAction, Windows PowerShell will stop the script execution at the point an error occurs. When these errors occur, they are considered “terminating errors.”

As an example, if you want to stop the execution of your Windows PowerShell script when an error occurs during a call to Stop-Process, you can simply add the -ErrorAction parameter and use the value Stop:

Stop-Process -Name invalidprocess -ErrorAction Stop;

Image of command output

If you want to suppress the errors from being displayed, you can use the value SilentlyContinue, for example:

Stop-Process -Name invalidprocess -ErrorAction SilentlyContinue;

Image of command output

You can also combine the ErrorAction and ErrorVariable parameters, such as in this example:

Stop-Process –Name invalidprocess -ErrorAction SilentlyContinue -ErrorVariable ProcessError;

If ($ProcessError) {

    ####### Something went wrong

}

Image of command output

We have explored the use of two Windows PowerShell common parameters, ErrorAction and ErrorVariable. I hope that this post has enlightened you about the use of these variables and how to use them to direct the execution flow of your scripts. Thank you for reading, and I will see you next time!

~Trevor

Thank you, Trevor, for taking the time to write this explanation and sharing it with our readers. You can reach Trevor on Twitter (https://twitter.com/pcgeek86) or follow him on his blog, Trevor Sullivan's Tech Room, Minding the gap between administration and development.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon