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.
- 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"
- 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.
- 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.