locked
Script to Enable/Disable Alert Rules RRS feed

  • Question

  • Hi Everyone,

    I need script to enable/disable alerts rules in azure, I will run it using azure automation. this is to prevent multiple alerts during deployment where resources(DTU, ETC.) are expected to peak.

    Thank you in advance!

    JK

    • Moved by Sheethal J S Tuesday, January 30, 2018 4:44 AM Best Suited Here
    Tuesday, January 30, 2018 2:52 AM

Answers

  • From the research I have done on this, it cannot be accomplished through PowerShell today with a simple command or property change as one would hope. Here are the ways that I have managed to come up with:

     

     

    • With a little bit of work there is a work around, You could simply remove the rules you care about during your deployment and then put them back after. This may sound like a cumbersome process but it really is not. If you make a template for each of the rules. Then you would just deploy the template to recreate it. This could all be done with a couple of lines of PowerShell and the template of course.

     

    Create a metric alert with a Resource Manager template:

    https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/monitoring-enable-alerts-using-template

     

    Hope that helps!

    ~Theo

    Monday, February 5, 2018 4:49 AM

All replies

  • Tuesday, January 30, 2018 4:19 AM
  • Thanks for you reply Natalya,

    Just a follow up question, it says that this works for activity log alerts, does it also work on alerts based on metrics? TIA

    Wednesday, January 31, 2018 2:37 AM
  • From the research I have done on this, it cannot be accomplished through PowerShell today with a simple command or property change as one would hope. Here are the ways that I have managed to come up with:

     

     

    • With a little bit of work there is a work around, You could simply remove the rules you care about during your deployment and then put them back after. This may sound like a cumbersome process but it really is not. If you make a template for each of the rules. Then you would just deploy the template to recreate it. This could all be done with a couple of lines of PowerShell and the template of course.

     

    Create a metric alert with a Resource Manager template:

    https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/monitoring-enable-alerts-using-template

     

    Hope that helps!

    ~Theo

    Monday, February 5, 2018 4:49 AM
  • @JkSinchioco

    I did end up writing a blog on doing this and was able to do it without using the templates. and just PowerShell, still a bit cumbersome but works perfectly. Take a look here:

    https://blogs.technet.microsoft.com/thbrown/2018/02/21/editupdate-an-azure-alert-rule-part-1-using-azure-powershell-cmdlets/

    Hope that helps!

    ~Theo

    Thursday, March 1, 2018 4:36 AM
  • I also had same requirement does anyone had achieved this?
    Wednesday, April 3, 2019 11:46 AM
  • It looks like the link to Theo's solution has changed.  This is the new link:

    https://gallery.technet.microsoft.com/scriptcenter/Disables-or-Enables-all-cfaf75dd

     

    And just in case anyone is looking for this solution in the future and the new link no longer works, this is Theo Browning's solution:

     

    This script gets all the rules in a given Azure Resource Group and either Disables them or Enables them depending on the presence of the Enable switch parameter. Default action (no -Enable) is to disable all the rules. Rules that are currently already disabled will stay disabled and vice versa depending on the presence of the Enable switch parameter.

     

    *NOTE*

    You must be logged into your AzureRMAccount and set to the subscription context that you want to use before calling this script.

     

    PowerShell

    #Simple use. 
    # Example: Disables all rules in the DevRG Resource Group. 
        PS C:\scripts>Disable_Enable_AllAlertRulesInRG.ps1 -ResourceGroupName DevRG 
     
    # Example: Enables all rules in the DevRG Resource Group. 
        PS C:\scripts>Disable_Enable_AllAlertRulesInRG.ps1 -ResourceGroupName DevRG -Enable 

     

    Two simple functions to either enable or disable the Alert Rules and calling is controlled by a switch statement. Download the full script to see the logic flow.

     

    PowerShell

    <# Function reads the array and disables all rules in the array. If a rule is already disabled, 
        it will stay disabled. This is the default function if the -Enable switch is not supplied. #>  
    function Disable-AllRules { 
        param([string]$RG) 
        foreach ($rule in $alertRules) { 
            Add-AzureRmMetricAlertRule ` 
                -ResourceGroupName $RG ` 
                -TargetResourceId $rule.Condition.DataSource.ResourceUri ` 
                -Location $rule.Location ` 
                -Name $rule.AlertRuleResourceName ` 
                -Description $rule.Description ` 
                -MetricName $rule.Condition.DataSource.MetricName ` 
                -Operator $rule.Condition.OperatorProperty ` 
                -Threshold $rule.Condition.Threshold ` 
                -WindowSize $rule.Condition.WindowSize ` 
                -TimeAggregationOperator $rule.Condition.TimeAggregation ` 
                -DisableRule; 

     

    Verified on the following platforms
    Windows 10 Yes
    Windows Server 2012 Yes
    Windows Server 2012 R2 Yes
    Windows Server 2008 R2 Yes
    Windows Server 2008 No
    Windows Server 2003 No
    Windows Server 2016 No
    Windows 8 No
    Windows 7 No
    Windows Vista No
    Windows XP No
    Windows 2000 No
    This script is tested on these platforms by the author. It is likely to work on other platforms as well. If you try it and find that it works on another platform, please add a note to the script discussion to let others know.

    Thursday, April 4, 2019 12:11 AM
  • Hello 

    Thanks for the script, that one works only for classic alerts? not for metric alerts is that correct? how can we make this work with new alerts(metric alerts created from loganalytics)

    For few classic alerts i am getting below error 

    Add-AzureRmMetricAlertRule : Cannot bind argument to parameter 'Operator' because it is null.
    At X:\test\Disable_Enable_AllAlertRulesInRG.ps1:71 char:23
    +             -Operator $rule.Condition.OperatorProperty `
    +                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-AzureRmMetricAlertRule], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Commands.Insights.Alerts.
       AddAzureRmMetricAlertRuleCommand

    Add-AzureRmMetricAlertRule : Cannot bind argument to parameter 'Operator' because it is null.
    At X:\test\Disable_Enable_AllAlertRulesInRG.ps1:71 char:23
    +             -Operator $rule.Condition.OperatorProperty `
    +                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Add-AzureRmMetricAlertRule], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Commands.Insights.Alerts.
       AddAzureRmMetricAlertRuleCommand


    Thursday, April 11, 2019 12:15 PM
  • please help on how to enable or disable all (Log Search Query alerts) loganalytics alerts at once by triggering runbooks
    Friday, April 12, 2019 8:07 AM