How do I configure Azure Monitor Alerts with the Azure Portal or Powershell?

To configure Azure Monitor Alerts with the Azure Portal, follow these steps:

  1. Navigate to the Azure portal and select your resource or resource group that you want to create an alert for.
  2. Click on “Alerts” under the “Monitoring” section in the left-hand menu.
  3. Click on “New Alert Rule” to create a new alert rule.
  4. Define the scope of the alert by selecting a resource, resource group, or subscription.
  5. Configure the conditions for the alert, including metrics, time range, aggregation, and threshold.
  6. Choose the action to be taken when the alert is triggered, such as sending an email, SMS message, or webhook notification.
  7. Review and confirm the alert rule settings, then save and enable the alert.

To configure Azure Monitor Alerts with PowerShell, you can use the New-AzMetricAlertRuleV2 cmdlet. Here’s an example script:





$resourceGroup = "myResourceGroup"
$resourceName = "myVM"
$ruleName = "myAlertRule"
$metricName = "Percentage CPU"
$operator = "GreaterThan"
$threshold = 90
$timeAggregation = "Average"
$timeWindow = "00:05:00"
$actionGroupId = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/microsoft.insights/actionGroups/{actionGroupId}"

New-AzMetricAlertRuleV2 `
    -ResourceGroup $resourceGroup `
    -TargetResourceId "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/microsoft.compute/virtualMachines/$resourceName" `
    -Name $ruleName `
    -MetricName $metricName `
    -Operator $operator `
    -Threshold $threshold `
    -TimeAggregation $timeAggregation `
    -WindowSize $timeWindow `
    -Actions $actionGroupId

This script creates a new alert rule for a virtual machine resource, based on the percentage of CPU utilization. When the CPU utilization is greater than 90% over a 5-minute window, the action group specified by the $actionGroupId variable is notified.

Author: tonyhughes