-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-ManagedIdentityGraphPermissions.ps1
More file actions
73 lines (61 loc) · 2.36 KB
/
Set-ManagedIdentityGraphPermissions.ps1
File metadata and controls
73 lines (61 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<#
.SYNOPSIS
Assign Microsoft Graph permissions to Azure Automation managed identity
.DESCRIPTION
This script assigns the necessary Microsoft Graph application permissions to an
Azure Automation managed identity for the Get-IntuneUsersAndDevicesFromGroups script.
Required permissions:
- DeviceManagementManagedDevices.Read.All
- Group.Read.All / Group.ReadWrite.All
- User.Read.All
- GroupMember.Read.All
- Device.Read.All
.PARAMETER managedIdentityName
Name of the Azure Automation Account (for system-assigned) or managed identity name (for user-assigned)
.NOTES
Authors:
Martin Bengtsson (https://imab.dk)
Christian Frohn (https://christianfrohn.dk)
Date: November 2025
Requires Global Administrator or Application Administrator role
Run this once per managed identity setup
#>
# Connect to Microsoft Graph as admin
Connect-MgGraph -Scopes "Application.Read.All", "AppRoleAssignment.ReadWrite.All"
# Get the managed identity service principal
# For system-assigned: use your Automation Account name
# For user-assigned: use your managed identity name
$managedIdentityName = "" # managed identity name
$managedIdentity = Get-MgServicePrincipal -Filter "displayName eq '$managedIdentityName'"
# Get Microsoft Graph service principal
$graphSP = Get-MgServicePrincipal -Filter "appId eq '00000003-0000-0000-c000-000000000000'"
# Define required permissions for your script
$requiredPermissions = @(
"DeviceManagementManagedDevices.Read.All",
"Group.Read.All",
"Group.ReadWrite.All",
"User.Read.All",
"GroupMember.Read.All",
"Device.Read.All"
)
# Assign each permission
foreach ($permissionName in $requiredPermissions) {
$appRole = $graphSP.AppRoles | Where-Object {
$_.Value -eq $permissionName -and $_.AllowedMemberTypes -contains "Application"
}
if ($appRole) {
try {
New-MgServicePrincipalAppRoleAssignment `
-ServicePrincipalId $managedIdentity.Id `
-PrincipalId $managedIdentity.Id `
-ResourceId $graphSP.Id `
-AppRoleId $appRole.Id `
-ErrorAction Stop
Write-Host "[OK] Assigned: $permissionName" -ForegroundColor Green
}
catch {
Write-Warning "Failed to assign $permissionName : $_"
}
}
}
Disconnect-MgGraph