How to create Azure Monitor - Alerts for virtual machine using Powershell scripts.

ryan t 0 Reputation points
2025-10-16T10:50:43.03+00:00

I want to create Azure Monitor - Alerts for virtual machine disk utilization, CPU usage etc.

Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
{count} votes

1 answer

Sort by: Most helpful
  1. Jose Benjamin Solis Nolasco 6,051 Reputation points
    2025-10-16T12:35:26.6733333+00:00

    Hello I hope you are doing well Ryan T

    You can follow this official Guide https://free.blessedness.top/en-us/azure/azure-monitor/alerts/alerts-create-rule-cli-powershell-arm

    PowerShell Script to Create a VM Metric Alert

    This script will first create an Action Group (which defines what happens when an alert is triggered, e.g., send an email) and then create the Metric Alert Rule for the Virtual Machine.

    Prerequisites

    Azure PowerShell Az Module: Ensure you have the Az module installed and the Az.Monitor component is available.

    PowerShell

    Install-Module
    

    Authenticate: Log in to your Azure account.

    PowerShell

    Connect-AzAccount
    

    Required Information: You'll need the following values:

    $ResourceGroupName: The name of the resource group where your VM is located.

      `$VMName`: The name of your Virtual Machine.
      
         `$AlertRuleName`: A unique name for your alert rule.
         
            `$ActionGroupName`: A name for the Action Group.
            
               `$EmailAddress`: The email address to receive notifications.
               
    

    Example Script: High CPU Alert

    PowerShell

    # --- Configuration Variables ---
    

    Key Components Explained

    Variable/Cmdlet Purpose Example Value for High CPU Alert
    Get-AzVM Retrieves the VM object to get its Resource ID. $VM.Id
    Get-AzVM Retrieves the VM object to get its Resource ID. $VM.Id
    Set-AzActionGroup Creates or updates the action group that defines the notification method (e.g., email, webhook, runbook). $ActionGroupId
    New-AzMetricAlertRuleV2Criteria Defines the logic for the alert condition. MetricName: "Percentage CPU", Threshold: 90, Operator: "GreaterThan"
    Add-AzMetricAlertRuleV2 Creates or updates the new metric alert rule. -TargetResourceId: The ID of the VM.
    -WindowSize The period over which the metric data is evaluated. (e.g., PT5M for 5 minutes) "PT5M"
    -Frequency How often the alert should check the condition. (e.g., PT1M for 1 minute) "PT1M"
    -Severity The impact level of the alert (0 is highest). 3 (Informational/Warning)

    Other Common Metrics

    You can change the -MetricName in the New-AzMetricAlertRuleV2Criteria cmdlet for other common VM metrics:

    Metric for VM Host (Platform) Description
    Percentage CPU Average CPU utilization.
    Percentage CPU Average CPU utilization.
    Network In Total Total bytes received on all network interfaces.
    Disk Read Bytes/sec Average number of bytes read from disk.
    Disk Write Bytes/sec Average number of bytes written to disk.

    For disk utilization and memory metrics from inside the OS (guest-level monitoring), you need to ensure the Azure Monitor Agent (AMA) is installed and configured to collect these metrics and send them to the Azure Monitor Metric database. If AMA is set up, common guest OS metrics for Windows/Linux are also available under the same process.

    I hope this helps some information were organízate using AI.

    😊 If my answer helped you resolve your issue, please consider marking it as the correct answer. This helps others in the community find solutions more easily. Thanks!


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.