[英]How to fail an Azure Automation Runbook?
Is there any way to make an Azure Automation Runbook end with a status of 'Failed'?有什么方法可以使 Azure 自动化 Runbook 以“失败”状态结束? Even when exceptions are thrown, the runbook still ends in a status of 'Complete'.
即使抛出异常,Runbook 仍以“完成”状态结束。 You can go into the runbook job with status of "Complete" and see the exceptions thrown.
您可以进入状态为“完成”的 Runbook 作业并查看抛出的异常。
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"?但是,是否有一种方法可以显式使 Azure Runbook 失败,例如,如果您捕获异常并希望整个 Runbook 失败,以便状态将以“失败”结尾?
In the catch
block, you need to use throw
statement again.在
catch
块中,您需要再次使用throw
语句。
Sample code in runbook: 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:运行完成后,在作业状态:
我在所有 PowerShell 脚本中使用 $errorActionPreference = "Stop",包括 Azure 自动化 Runbook,否则正常的 PowerShell 处理(“继续”)将导致无法处理和报告错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.