How to create Azure Monitor alert in Powershell Az.Monitor with Custom Properties

Grayson Williams 0 Reputation points Microsoft Employee
2024-10-08T18:07:07.4733333+00:00

I have azure monitor alerts automatically created using the PowerShell library Az.Monitor, but I don't see anywhere in the library on how to include Custom Properties as are available in the Portal under Details and Advanced Options.

Also worth noting, I don't know how to set the automatically resolve alert flag either.

Can anyone please help share how to do this or confirm if it is not possible?User's image

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

1 answer

Sort by: Most helpful
  1. Vinodh247 23,581 Reputation points MVP
    2024-10-09T00:35:04.28+00:00

    Hi Grayson Williams,

    Thanks for reaching out to Microsoft Q&A.

    Creating Azure Monitor alerts with custom properties using PowerShell can be a bit tricky since the Az.Monitor module doesn’t directly support adding custom properties as you can in the Azure Portal. However, you can achieve this by using Azure Resource Manager (ARM) templates or by leveraging the REST API.

    1. Using ARM Template

    You can define custom properties and configure "Automatically resolve alerts" using ARM templates, which you can then deploy via PowerShell.

    Here’s an example of how to set up an alert rule with custom properties:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "resources": [
        {
          "type": "Microsoft.Insights/metricAlerts",
          "apiVersion": "2018-03-01",
          "location": "global",
          "properties": {
            "severity": 3,
            "enabled": true,
            "autoMitigate": true,  // Automatically resolves the alert
            "criteria": {
              ...
            },
            "customProperties": {
              "test": "123"  // Custom property
            }
          }
        }
      ]
    }
    
    
    

    Deploy it using powershell:

    New-AzResourceGroupDeployment -ResourceGroupName "yourResourceGroupName" -TemplateFile "path-to-your-template.json"

    1. Using the Azure REST API

    The Azure REST API for alerts allows more detailed configuration, including custom properties. You can call the API from PowerShell.

    Here’s an example of setting custom properties via the API:

    $uri = "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Insights/metricAlerts/{alertRuleName}?api-version=2018-03-01"
    $body = @{
        "properties" = @{
            "severity" = 3
            "enabled" = $true
            "autoMitigate" = $true  # Automatically resolves the alert
            "customProperties" = @{
                "test" = "123"  # Custom property
            }
        }
    }
    $authHeader = @{
        Authorization = "Bearer $($token)"
    }
    Invoke-RestMethod -Uri $uri -Method Put -Body ($body | ConvertTo-Json) -Headers $authHeader
    
    
    

    This REST API call would allow you to include the same configuration as you would in the portal.

    1. PowerShell Module Limitation

    As of now, there is no support in the Az.Monitor module to directly include custom properties or set the "Automatically resolve alerts" flag. These functionalities may be added in future versions of the module, but for now, using ARM templates or the Azure REST API is the best workaround.

    Please 'Upvote'(Thumbs-up) and 'Accept' as an answer if the reply was helpful. This will benefit other community members who face the same issue.

    1 person found this answer helpful.

Your answer

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