I have the following code
# Variables
$task3storageAccountName = "testRG"
$actionGroupName = "Storage Admins"
$actionGroupShortName = "SA Admins"
$notificationName = "Email storage admins"
$emailAddress = "[email protected]" # Replace with your actual email
# Get the Storage Account and its resource ID (this will be used in the alert rule)
$storageAccount = Get-AzStorageAccount -Name $task3storageAccountName -ResourceGroupName $resourceGroupName
Write-Host "For task 3 the storage account '$task3storageAccountName' will be used.`nResource ID: $($storageAccount.Id)"
$storageAccount | Format-List
# Create receiver object
$email1 = New-AzActionGroupEmailReceiverObject -EmailAddress $emailAddress -Name "Name"
$sms1 = New-AzActionGroupSmsReceiverObject -CountryCode '61' -Name user2 -PhoneNumber '00000000'
# Create action group
# https://learn.microsoft.com/en-us/powershell/module/az.monitor/new-azactiongroup?view=azps-14.0.0&viewFallbackFrom=azps-13.3.0
$actionGroup=New-AzActionGroup `
-Name $actionGroupName `
-ResourceGroupName $resourceGroupName `
-Location "global" `
-GroupShortName $actionGroupShortName `
-EmailReceiver $email1 `
-SmsReceiver $sms1
$alertRuleName = "Storage account key generation failed"
$location = "global"
$categoryCondition = New-AzActivityLogAlertAlertRuleAnyOfOrLeafConditionObject `
-Field "category" `
-Equal "Administrative"
# Operation name filter
$operationCondition = New-AzActivityLogAlertAlertRuleAnyOfOrLeafConditionObject `
-Field "operationName" `
-Equal "Microsoft.Storage/storageAccounts/regenerateKey/action"
# Status filter
$statusCondition = New-AzActivityLogAlertAlertRuleAnyOfOrLeafConditionObject `
-Field "status" `
-Equal "Failed"
# Create the alert
# There's a bug in the Az module that prevents the use of the -Action parameter.
$alert=New-AzActivityLogAlert `
-Name $alertRuleName `
-ResourceGroupName $resourceGroupName `
-Action $actionGroup `
-Condition @($categoryCondition, $operationCondition, $statusCondition) `
-Location $location `
-Scope $storageAccount.Id `
-Enabled:$true
The issue is with the cmdlet New-AzActivityLogAlert and the parameter Action. There seems to be a bug that prevents the Activity Log Alert to have the designated Action Group.
A temporary fix for this issue is to run az monitor activity-log alert action-group add -n test2 -g $resourceGroupName --action $actionGroup.Id
But this requires login both via CLI and PowerShell.


