0

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
    }
Blue Clouds
  • 7,295
  • 4
  • 71
  • 112

2 Answers2

0

You can refer to THIS similar question. The answer should hold good for your case as well.

Anupam Chand
  • 2,209
  • 1
  • 5
  • 14
0

Your code is wrong. This is the correct one. The throw will make the runbook fail. The code in question is applicable when running it inside a VM(not through automation runbook)

$result=Invoke-AzVMRunCommand -ResourceGroupName $VMResourceGroupName -VMName $VMname -ScriptPath ScriptToRun.ps1 -CommandId 'RunPowerShellScript'
if(!$result)
{
    throw "Error Occured"
}
else
{
    Write-Output "Succeeded"
}
Blue Clouds
  • 7,295
  • 4
  • 71
  • 112