locked
Script to check for alerts enabled on each resource in a resource group RRS feed

  • Question

  • Hi,

    I was wondering would anyone know of a PowerShell script that checks to see if alerts are configured for each resource in a resource group, so that if there isn't (a value of null) this info can be passed into a spreadsheet/email and an Azure admin will know what resources in resource groups currently don't have any alerting configured? I think such a script would be very useful in an Azure subscription where there are numerous resource groups. 

    As always any advice greatly appreciated not just the script but on best practices here. 

    Friday, February 9, 2018 5:37 PM

All replies

  • You may refer to the following below links:
    https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/insights-powershell-samples
    https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/insights-alerts-powershell
    https://blogs.msdn.microsoft.com/cloud_solution_architect/2016/02/26/retrieving-resource-metrics-and-creating-alert-rules-via-azure-powershell/
    -------------------------------------------------------------------------------------------------------------
    Do click on "Mark as Answer" on the post that helps you and vote it as helpful, this can be beneficial to other community members. 
    Saturday, February 10, 2018 12:08 PM
  • Thanks for the reply but its not what I am currently looking for. 
    Monday, February 12, 2018 1:45 PM
  • @IvorJ

    That is actually pretty simple as the management REST API allows for  that, instead of passing in a rule name you just call alertrules with no value. Here is an example (tested and works, just paste in your subscription Id and resource group name and pass a auth header to the function).

    function Get-AnyAllRules

    {

        param([HastTable]$authHeader)

        # Format looks like this:

        $Uri = "https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/microsoft.insights/alertrules?api-version=2016-03-01";

        $response = Invoke-RestMethod -Uri $Uri -Method GET -Headers $authHeader

        $responseJSON = ConvertTo-Json -InputObject $response;

        return $responseJSON;

    }

    Viola you will get a JSON with all rules or none if there aren't any.

    Let me know if you need some auth code and I would be happy to give an example of that as well.

    (I probably should just blog about this... maybe when I get some free time!)

    ~Theo



    Tuesday, February 13, 2018 5:05 AM
  • @IvorJ

    I actually had this come up again this week and I found another creative approach and thought I would share with you, give this a try:

    $RG = "Your Resource Group Name Here";

    Find-AzureRmResource -ResourceGroupNameEquals $RG | foreach{if((Get-AzureRmAlertRule -ResourceGroup $RG -TargetResourceId $_.ResourceId -WarningAction SilentlyContinue) -eq $null){"No rule configured: {0}" -f $_.ResourceId}else{"Has rule: {0}" -f $_.ResourceId}}

    Output will look like so:

    No rule configured: /subscriptions/c0ff33-f00d-473c-9327-1l0v3c00ff33/resourceGroups/DevEnv/providers/Microsoft.Network/networkSecurityGroups/DevBox-nsg
    No rule configured: /subscriptions/c0ff33-f00d-473c-9327-1l0v3c00ff33/resourceGroups/DevEnv/providers/Microsoft.Storage/storageAccounts/devenvstrg
    No rule configured: /subscriptions/c0ff33-f00d-473c-9327-1l0v3c00ff33/resourceGroups/DevEnv/providers/Microsoft.Network/virtualNetworks/DevEnv-vnet
    Has rule: /subscriptions/c0ff33-f00d-473c-9327-1l0v3c00ff33/resourceGroups/DevEnv/providers/Microsoft.Compute/virtualMachines/DevServer
    No rule configured: /subscriptions/c0ff33-f00d-473c-9327-1l0v3c00ff33/resourceGroups/DEVENV/providers/Microsoft.Compute/disks/DevServer_OsDisk_1_92f9a63b9d394753869651a140598210
    No rule configured: /subscriptions/c0ff33-f00d-473c-9327-1l0v3c00ff33/resourceGroups/DevEnv/providers/Microsoft.Network/networkInterfaces/devserver972


    Hope that helps!

    ~Theo

    Monday, February 19, 2018 6:33 PM
  • Blogged about this and turned the above into full script:

    https://blogs.technet.microsoft.com/thbrown/2018/02/20/check-all-resources-in-a-resource-group-for-the-presence-of-a-configured-alert-rule-with-powershell/

    ~Theo

    Tuesday, February 20, 2018 5:42 AM