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.

Stream

Description

Published

Test

Error

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.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Disable feedback surveys in Power Platform

If you have worked in Microsoft 365 PowerApps or Power Automate, you might be familiar with feedback survey which pops up quite often. Microsoft wants your feedback on how likely are your to recommend Power Automate to a friend or colleague? Well, I ran into a conversation in twitter about this survey. Apparently I learnt […]

Read More

Follow up on a Teams message – Automation

If you get a lot of Instant messages in Teams for help or to answer questions even when you are in a meeting or status set to Focusing, this automation is for you! Most of us IT workers get IM’ed a lot these days with everyone working from home and new changes to Office 365 […]

Read More

The underlying connection was closed: An unexpected error occurred on a send

PowerShell error – The underlying connection was closed: An unexpected error occurred on a send, while trying to run CSOM PowerShell code on SharePoint online sites. Same error while trying to use PnP cmdltes From network trace we are able to figure out that server is closing the TCP connection forcefully. Reason being a mismatch […]

Read More