Edit/Update an Azure Alert Rule: Part 1 = Using Azure PowerShell Cmdlets.

     Azure allows us to create Metric Alert Rules for a resource to trigger when something we want to know about happens like CPU being to high for a certain time, available memory dropping too low, or an Azure Function Starting (much more). We will not get much deeper that here about alert types but, you can read more here: Azure Metric Alert Rule . Now, say you need to complete a task (maintenance etc.) and this task is going to do something that triggers the rule(s) over and over. Well you don't want this sending you a ton of emails or lighting up your Health Status Board or whatever you have the alert(s) setup to do as a notification(s). So how do you turn it off? Well, there is the manual way of logging into the portal, navigating to the alert and then finally clicking on the disable button. Ugg manual clicks! And what if you have like 20,30,40 or more alerts setup, well then 3 hours later once you have clicked them all to disabled you can start your maintenance... Ok, maybe not 3 hours but definitely not fast... So automation is the only way to go here.

     Now unfortunately there isn't an easy PowerShell Set- command to do this for us. If we go look at the AlertRule commands you'll see our options are Add, Get, New, and Remove:

Now if you use Add-AzureRMMetricAlertRule and there is a rule present present with the same name then it will over write the present rule with the updated settings. However, you cannot do something like we normally use in PowerShell doing a "Get-AzureRmAlertRule | Add-AzureRmMetricAlertRule -DisableRule" as the properties for each cmdlets are not one to one mappings. So one needs to be a little creative to approach this. The approach we will use is to get all the Alert Rules for a given Resource Group and save them in a collection. Then then through the power of the pipeline we will remap all the relevant properties from the Get to the correct counter part property of the Add cmdlet.

FYI: In part 2 we are going to do this in C# using the REST API for Azure Management.

Ok enough talk, let's see the code:

Happy coding and Azure on my friends!

Full script can be downloaded from the Microsoft Script Center: Disable_Enable_AllAlertRulesInRG.ps1

~Theo