Error handling in Azure PowerShell Runbooks

Error handling in Azure PowerShell Runbooks should be handled carefully if not the runbook with continue to Completed state even if there is an Error in the script. Unlike exception, Runbook execution will continue after  an Error  by default.

Windows PowerShell has multiple streams of output. Runbooks in Azure work differently in each of these streams. Error, Debug, Output, Progress, Verbose & Warning are different streams. Each work differently. Refer to this Microsoft document for more information.

In this article I want to highlight Error stream which is most commonly used and an important aspect of error handling.






Error message intended for the user. Unlike with an exception, the runbook continues after an error message by default.

Written to job history

Displayed in Test output pane

Like I mentioned above script execution will continue by default even after it encounters an Error. Error information will be displayed in output for user.

Error is considered a message stream. Unlike output stream, message streams communicate information to user and continue execution. If we want the runbook to stop execution if it encounters an error, we have to exclusively specify that it should suspend on error. It is done by setting a preference variable in Runbook.

ErrorActionPreference variable should be set to Stop to end the execution of Runbook upon encountering an Error.

Note: Try Catch blocks can only handle Terminating errors. We have to set ErrorActionPreference to handle non terminating errors.

Here is another tip, add -ErrorAction “Stop” to the cmdlet to stop execution of script if an error is encountered.