Skip to content

Commit 0caa7ae

Browse files
Merge pull request #277839 from jacegummersall/patch-36
Update action-groups.md
2 parents a900156 + c9e33d3 commit 0caa7ae

File tree

1 file changed

+71
-33
lines changed

1 file changed

+71
-33
lines changed

articles/azure-monitor/alerts/action-groups.md

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -559,60 +559,98 @@ If you use the webhook action, your target webhook endpoint must be able to proc
559559

560560
### Secure webhook PowerShell script
561561

562+
> [!NOTE]
563+
>
564+
>Pre-requisites: https://learn.microsoft.com/powershell/microsoftgraph/installation?view=graph-powershell-1.0
565+
566+
#### How to run?
567+
568+
1. Copy and paste the script below to your machine
569+
2. Replace your tenantId, and the ObjectID in your App Registration
570+
3. Save as *.ps1
571+
4. Open the PowerShell command from your machine, and run the *.ps1 script
572+
562573
```PowerShell
563-
Connect-AzureAD -TenantId "<provide your Azure AD tenant ID here>"
564-
# Define your Azure AD application's ObjectId.
565-
$myAzureADApplicationObjectId = "<the Object ID of your Azure AD Application>"
566-
# Define the action group Azure AD AppId.
567-
$actionGroupsAppId = "461e8683-5575-4561-ac7f-899cc907d62a"
568-
# Define the name of the new role that gets added to your Azure AD application.
574+
Write-Host "================================================================================================="
575+
$scopes = "Application.ReadWrite.All"
576+
$myTenantId = "<<Customer's tenant id>>"
577+
$myMicrosoftEntraAppRegistrationObjectId = "<<Customer's object id from the app registration>>"
569578
$actionGroupRoleName = "ActionGroupsSecureWebhook"
570-
# Create an application role with the given name and description.
579+
$azureMonitorActionGroupsAppId = "461e8683-5575-4561-ac7f-899cc907d62a" # Required. Do not change.
580+
581+
Connect-MgGraph -Scopes $scopes -TenantId $myTenantId
582+
571583
Function CreateAppRole([string] $Name, [string] $Description)
572584
{
573-
$appRole = New-Object Microsoft.Open.AzureAD.Model.AppRole
574-
$appRole.AllowedMemberTypes = New-Object System.Collections.Generic.List[string]
575-
$appRole.AllowedMemberTypes.Add("Application");
576-
$appRole.DisplayName = $Name
577-
$appRole.Id = New-Guid
578-
$appRole.IsEnabled = $true
579-
$appRole.Description = $Description
580-
$appRole.Value = $Name;
585+
$appRole = @{
586+
AllowedMemberTypes = @("Application")
587+
DisplayName = $Name
588+
Id = New-Guid
589+
IsEnabled = $true
590+
Description = $Description
591+
Value = $Name
592+
}
581593
return $appRole
582594
}
583-
# Get your Azure AD application, its roles, and its service principal.
584-
$myApp = Get-AzureADApplication -ObjectId $myAzureADApplicationObjectId
595+
596+
$myApp = Get-MgApplication -ApplicationId $myMicrosoftEntraAppRegistrationObjectId
585597
$myAppRoles = $myApp.AppRoles
586-
$actionGroupsSP = Get-AzureADServicePrincipal -Filter ("appId eq '" + $actionGroupsAppId + "'")
598+
$myActionGroupServicePrincipal = Get-MgServicePrincipal -Filter "appId eq '$azureMonitorActionGroupsAppId'"
599+
587600
Write-Host "App Roles before addition of new role.."
588-
Write-Host $myAppRoles
589-
# Create the role if it doesn't exist.
590-
if ($myAppRoles -match "ActionGroupsSecureWebhook")
601+
foreach ($role in $myAppRoles) { Write-Host $role.Value }
602+
603+
if ($myAppRoles.Value -contains $actionGroupRoleName)
591604
{
592-
Write-Host "The Action Group role is already defined.`n"
605+
Write-Host "The Action Group role is already defined. No need to redefine.`n"
606+
# Retrieve the application again to get the updated roles
607+
$myApp = Get-MgApplication -ApplicationId $myMicrosoftEntraAppRegistrationObjectId
608+
$myAppRoles = $myApp.AppRoles
593609
}
594610
else
595611
{
596-
$myServicePrincipal = Get-AzureADServicePrincipal -Filter ("appId eq '" + $myApp.AppId + "'")
597-
# Add the new role to the Azure AD application.
612+
Write-Host "The Action Group role is not defined. Defining the role and adding it."
598613
$newRole = CreateAppRole -Name $actionGroupRoleName -Description "This is a role for Action Group to join"
599-
$myAppRoles.Add($newRole)
600-
Set-AzureADApplication -ObjectId $myApp.ObjectId -AppRoles $myAppRoles
614+
$myAppRoles += $newRole
615+
Update-MgApplication -ApplicationId $myApp.Id -AppRole $myAppRoles
616+
617+
# Retrieve the application again to get the updated roles
618+
$myApp = Get-MgApplication -ApplicationId $myMicrosoftEntraAppRegistrationObjectId
619+
$myAppRoles = $myApp.AppRoles
601620
}
602-
# Create the service principal if it doesn't exist.
603-
if ($actionGroupsSP -match "AzNS AAD Webhook")
621+
622+
$myServicePrincipal = Get-MgServicePrincipal -Filter "appId eq '$($myApp.AppId)'"
623+
624+
if ($myActionGroupServicePrincipal.DisplayName -contains "AzNS AAD Webhook")
604625
{
605626
Write-Host "The Service principal is already defined.`n"
627+
Write-Host "The action group Service Principal is: " + $myActionGroupServicePrincipal.DisplayName + " and the id is: " + $myActionGroupServicePrincipal.Id
606628
}
607629
else
608630
{
609-
# Create a service principal for the action group Azure AD application and add it to the role.
610-
$actionGroupsSP = New-AzureADServicePrincipal -AppId $actionGroupsAppId
631+
Write-Host "The Service principal has NOT been defined/created in the tenant.`n"
632+
$myActionGroupServicePrincipal = New-MgServicePrincipal -AppId $azureMonitorActionGroupsAppId
633+
Write-Host "The Service Principal is been created successfully, and the id is: " + $myActionGroupServicePrincipal.Id
611634
}
612-
New-AzureADServiceAppRoleAssignment -Id $myApp.AppRoles[0].Id -ResourceId $myServicePrincipal.ObjectId -ObjectId $actionGroupsSP.ObjectId -PrincipalId $actionGroupsSP.ObjectId
613-
Write-Host "My Azure AD Application (ObjectId): " + $myApp.ObjectId
635+
636+
# Check if $myActionGroupServicePrincipal is not $null before trying to access its Id property
637+
# Check if the role assignment already exists
638+
$existingRoleAssignment = Get-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $myActionGroupServicePrincipal.Id | Where-Object { $_.AppRoleId -eq $myApp.AppRoles[0].Id -and $_.PrincipalId -eq $myActionGroupServicePrincipal.Id -and $_.ResourceId -eq $myServicePrincipal.Id }
639+
640+
# If the role assignment does not exist, create it
641+
if ($null -eq $existingRoleAssignment) {
642+
Write-Host "Doing app role assignment to the new action group Service Principal`n"
643+
New-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $myActionGroupServicePrincipal.Id -AppRoleId $myApp.AppRoles[0].Id -PrincipalId $myActionGroupServicePrincipal.Id -ResourceId $myServicePrincipal.Id
644+
} else {
645+
Write-Host "Skip assigning because the role already existed."
646+
}
647+
648+
Write-Host "myServicePrincipalId: " $myServicePrincipal.Id
649+
Write-Host "My Azure AD Application (ObjectId): " $myApp.Id
614650
Write-Host "My Azure AD Application's Roles"
615-
Write-Host $myApp.AppRoles
651+
foreach ($role in $myAppRoles) { Write-Host $role.Value }
652+
653+
Write-Host "================================================================================================="
616654
```
617655
### Migrate Runbook action from "Run as account" to "Run as Managed Identity"
618656
> [!NOTE]

0 commit comments

Comments
 (0)