I have this command running inside an automation runbook. Sometimes if the VM is stopped the command gives error. Based on this error I want the Status of automation runbook be 'Failed' instead of completed. But the runbook is giving 'Completed' status. Because of this I am unable to trigger an alert using Azure Monitor(which checks for status value). How to set the runbook status to 'Failed' on such errors in the runbook?
$result=Invoke-AzVMRunCommand -ResourceGroupName $VMResourceGroupName -VMName $VMname -ScriptPath ScriptToRun.ps1 -CommandId 'RunPowerShellScript'
if ($result.value.Message -like '*error*')
{
throw $result.value.Message
}