I will show how to disable Azure Alerts using a script. To tell the truth, I found this amazingly difficult even though there is documentation about how to do it.
My first attempt to disable Azure Metric Alerts
In the first place creating a powershell script from Manage your alert rules – Azure Monitor | Microsoft Learn runs with some errors.
Get-AzMetricAlertRuleV2 -ResourceGroupName MyResourceGroup | Add-AzMetricAlertRuleV2 -DisableRule
While this was successful for normal metric alert , dynamic metric alerts fail with “Add-AzMetricAlertRuleV2: Cannot validate argument on parameter ‘TargetResourceRegion’. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.”
My second attempt to disable Azure Metric Alerts
Secondly creating an Azure CLI script from Manage your alert rules – Azure Monitor | Microsoft Learn you can disable dynamic alerts.
az monitor metrics alert update --enabled false --name "MyMetricAlerts_dynamic_alert" --resource-group MyResourcegroup
Nevertheless, running this script to update all alerts in a resource group does not work.
az monitor metrics alert list --resource-group au-edi-uat-rg | ForEach-Object {az monitor metrics alert update --enabled false --name $_.name --resource-group au-edi-uat-rg}
I think the issue is because the retrieving the name of the alert name from the list out put array does not work in my hands.
The final solution
Finally, this scripts disables both normal and dynamic metric alerts. This uses a combination of PowerShell and Azure CLI.
Get-AzMetricAlertRuleV2 -ResourceGroupName au-edi-uat-rg | ForEach-Object {az monitor metrics alert update --enabled false --name $_.Name --resource-group MyResourceGroup}
I hope saves someone else some time.