Skip to content

Instantly share code, notes, and snippets.

@mattbalzan
Created January 22, 2025 17:20
Show Gist options
  • Save mattbalzan/01a9f5f6cd7cad1d593bf8f857ce6465 to your computer and use it in GitHub Desktop.
Save mattbalzan/01a9f5f6cd7cad1d593bf8f857ce6465 to your computer and use it in GitHub Desktop.
Set Azure MSI Graph permissions
$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