-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-permission-to-managed-identity.ps1
46 lines (40 loc) · 1.94 KB
/
api-permission-to-managed-identity.ps1
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
<#
.SYNOPSIS
This example shows how to assign Graph API or SharePoint API permissions to a Managed Identity.
.DESCRIPTION
This example shows how to assign API permissions to a Managed Identity.
.NOTES
Author: Øyvind Nilsen ([email protected])
Creation Date: September 8th 2023
.LINK
https://github.com/NTNU-IT-M365/ms-graph-snippets
#>
Connect-MgGraph -Scopes "Application.ReadWrite.All"
# Get Powershell Graph API Service Principal:
$GraphId = '00000003-0000-0000-c000-000000000000' # This is a hard coded ID for Powershell Graph API. It is the same for all tenants. Use this for assigning Graph API permissions.
$SharePointId = '00000003-0000-0ff1-ce00-000000000000' # Use this for assigning SharePoint API permissions.
# Choose API to assign permissions to:
$APIid = $GraphId #Change this to $SharePointId if you want to assign SharePoint API permissions instead of Graph API permissions.
# Get the service principal for the selected API:
$uri = "https://graph.microsoft.com/v1.0/servicePrincipals/?`$filter=AppId eq '${APIid}'"
$APIprincipal = (Invoke-MgGraphRequest -Uri $uri).value
# Get Managed Identity: Insert the Object ID for your Managed Identity. You can find it in your automation account.
# N.B. Application ID won't work!
$ManagedIdentityId = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'
# Specify what permission scopes you want to assign:
$Permissions = @(
'Sites.Selected',
'User.Read.All'
)
# Get GUID for the specified permissions (AppRoles) from the API principal:
$AppRoles = $APIprincipal.AppRoles | Where-Object { $Permissions -contains $_.Value }
# Assign the permissions (AppRoles) to the Managed Identity:
$uri = "https://graph.microsoft.com/v1.0/servicePrincipals/$($APIprincipal.id)/appRoleAssignedTo"
foreach ($AppRole in $AppRoles) {
$body = @{
"principalId" = $ManagedIdentityId
"resourceId" = $APIprincipal.id
"appRoleId" = $AppRole['id']
}
Invoke-MgGraphRequest -Method POST -Uri $uri -Body $body
}