Azure Monitor Alert when VM is not available/Shutdown Down

Assuming you connected Virtual machine to Log Analytics already and sending heartbeats. Now You can run below powershell to get alerted when VM is not sending hearbeats . This scernario will cover you in boot reboot and shutdown events as well.

$LogAnalyticsResourceGroup= “Enter Log Analytics Resource Group”
$workspaceName=”Enter Log Analytics WorkspaceName”

$workspace = Get-AzOperationalInsightsWorkspace -ResourceGroupName $LogAnalyticsResourceGroup -Name $workspaceName

$dimension = New-AzMetricAlertRuleV2DimensionSelection -DimensionName “Computer” -ValuesToInclude “*”

$criteria = New-AzMetricAlertRuleV2Criteria -MetricName “Heartbeat” `
-DimensionSelection $dimension `
-TimeAggregation Total `
-Operator LessThan `
-threshold 3 `

Add-AzMetricAlertRuleV2 -Name “VM HeartBeat Alert” `
-ResourceGroupName $LogAnalyticsResourceGroup `
-WindowSize 00:05:00 `
-Frequency 00:01:00 `
-TargetResourceId $workspace.ResourceId `
-Condition $criteria `
-Severity 3