7

Is there any way to make an Azure Automation Runbook end with a status of 'Failed'? Even when exceptions are thrown, the runbook still ends in a status of 'Complete'. You can go into the runbook job with status of "Complete" and see the exceptions thrown.

However, is there a way to explicitly fail an Azure Runbook, for example if you catch an exception and want the entire runbook to fail, so that the status will end in "Failed"?

Birdman
  • 1,404
  • 5
  • 22
  • 49

2 Answers2

5

In the catch block, you need to use throw statement again.

Sample code in runbook:

try
{
   $wc = new-object System.Net.WebClient
   $wc.DownloadFile("http://www.contoso.com/MyDoc.doc")
}
catch
{
    # explicitly use throw here
    throw "I have some errors."
}

After running completed, in the job status:

enter image description here

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
5

I use $errorActionPreference = "Stop" in all PowerShell scripts, including Azure Automation Runbooks otherwise normal PowerShell handling ("Continue") will cause errors to not be handled and reported.

Badajoz95
  • 394
  • 4
  • 6