Azure Azure PowerShell Azure Service Health Cloud Shell

Create an Azure Monitor action group with Azure PowerShell

Azure Monitor, Microsoft’s built-in monitoring service, allows you to monitor and gain more visibility into the state of your resources from a single place in the Azure portal. In this way this service can help you to quickly find and fix problems.

To notify users that an alert has been triggered, Azure Monitor and also Azure Service Health uses action groups. This feature allows an owner of an Azure subscription to group a collection of actions to take when an alert is triggered. Owners can create an action group with functions such as sending an email, SMS, Logic or Function App, as well as calling a webhook and re-use it across multiple alerts. Various alerts may use the same action group or different action groups depending on the user’s requirements. Action groups can be created through the Azure portal, but to automate the process you can also use Azure PowerShell.

In the below example a new action group, called email-ag is created. This will notify the added people (email addresses) by email whenever a specific event is triggered. For example, you can alert on metrics and logs, like Activity Log events, log search queries, or even the health of the underlying Azure platform (service issues). For the moment, you may have up to 1000 email actions in an action group. Also ensure that your email filtering is configured appropriately.

To use the script, copy it and adjust it for your own purpose. Save it as .ps1. These days I always use PowerShell scripts or cmdlets, directly in Azure Cloud Shell or via https://shell.azure.com

<#

.SYNOPSIS

A script used to create an Azure Monitor Action Group 

.DESCRIPTION

A script used to used to create an Azure Monitor Action Group. The Action Type used in this script is Email.

.NOTES

Filename:       Create_Azure_Monitor_Action_Group.ps1
Created:        26/11/2019
Last modified:  26/11/2019
Author:         Wim Matthyssen
PowerShell:     Azure Cloud Shell or Azure PowerShell
Version:        Install all latest modules if you use Azure PowerShell
Action:         Change variables were needed to fit your needs
Disclaimer:     This script is provided "As IS" with no warranties.

.EXAMPLE

.\Create_Azure_Monitor_Action_Group.ps1

.LINK

https://wmatthyssen.com/2019/11/26/create-an-azure-monitor-action-group-with-azure-powershell/

#>

## Variables

$emailReceiverName = "emailreceiver"
$emailAddress = "testemail@outlook.com"
$actionGroupName = "email-ag"
$actionGroupShortName = $actionGroupName
$rgName = "wm-demo-management-rg"
$tagName = "Environment"
$tagValue = "Demo"
$time = Get-Date -UFormat "%A %m/%d/%Y %R"
$foregroundColor1 = "Red"
$writeEmptyLine = "`n"
$writeSeperator = " - "

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Log on with your Azure account if your not using Azure Cloud Shell

# Login-AzureRmAccount

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Create a new Action Group Email receiver

$emailReceiver = New-AzureRmActionGroupReceiver -Name $emailReceiverName -EmailReceiver -EmailAddress $emailAddress

Write-Host ($writeEmptyLine + "# Action Group Receiver $emailReceiverName saved in memory" + $writeSeperator + $time)`
-foregroundcolor $foregroundColor1 $writeEmptyLine 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Create a new Action Group

$tag = New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"
$tag.Add($tagName,$tagValue)

Set-AzureRmActionGroup -Name $actionGroupName -ResourceGroup $rgName -ShortName $actionGroupShortName -Receiver $emailReceiver -Tag $tag

Write-Host ($writeEmptyLine + "# Action Group $actionGroupName created" + $writeSeperator + $time)`
-foregroundcolor $foregroundColor1 $writeEmptyLine 

## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



You can check all existing action groups in your subscription, by running the below cmdlet. In my example the previously created action group email-ag is shown.

## View available action groups

Get-AzureRmActionGroup | Select-Object Name


Like already said earlier, you can also add, validate or manage action groups through the Azure portal by opening Monitor, selecting Alerts and selecting Manage actions. For more information you can check out the Microsoft Docs page.



Hope this script comes in handy!

2 comments on “Create an Azure Monitor action group with Azure PowerShell

%d bloggers like this: