Created
January 22, 2025 17:20
-
-
Save mattbalzan/01a9f5f6cd7cad1d593bf8f857ce6465 to your computer and use it in GitHub Desktop.
Set Azure MSI Graph permissions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$TenantID = "<tenantID>" | |
$ManagedIdentity = "<MSI name>" | |
$Permissions = @("DeviceManagementManagedDevices.Read.All", "DeviceManagementManagedDevices.ReadWrite.All", "AuditLog.Read.All", "User.Read.All") | |
$GraphAppId = "00000003-0000-0000-c000-000000000000" | |
Connect-AzureAD -TenantId $TenantID | |
$ManagedIdentityServicePrincipal = (Get-AzureADServicePrincipal -Filter "displayName eq '$ManagedIdentity'") | |
$GraphServicePrincipal = Get-AzureADServicePrincipal -Filter "appId eq '$GraphAppId'" | |
foreach ($Permission in $Permissions) | |
{ | |
$AppRole = $GraphServicePrincipal.AppRoles | Where-Object {$_.Value -eq $Permission -and $_.AllowedMemberTypes -contains "Application"} | |
New-AzureAdServiceAppRoleAssignment -ObjectId $ManagedIdentityServicePrincipal.ObjectId -PrincipalId $ManagedIdentityServicePrincipal.ObjectId -ResourceId $GraphServicePrincipal.ObjectId -Id $AppRole.Id | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment