-
Notifications
You must be signed in to change notification settings - Fork 4
/
grant-adminConsentForServicePrincipal.ps1
73 lines (63 loc) · 3.47 KB
/
grant-adminConsentForServicePrincipal.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
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
Enforces / syncs all permissions from a given multi-tenant application in a given tenant into it's representation (service principal) in another tenant.
Useful for MSP's / CSP's / App Publishers.
Requires DelegatedPermissionGrant.ReadWrite.All and AppRoleAssignment.ReadWrite.All Graph Permissions.
New-GraphGetRequest and New-GraphPostRequest functions not included.
.NOTES
author: Jos Lieben / jos@lieben.nu
copyright: Lieben Consultancy, free to (re)use, keep headers intact
disclaimer: https://www.lieben.nu/liebensraum/contact/#disclaimer-and-copyright
site: https://www.lieben.nu
Created: 29/05/2023
Updated: See Gitlab
#>
$sourceTenantId = "GUID-OR-DOMAIN"
$targetTenantId = "GUID-OR-DOMAIN"
$sourceSpnId = "GUID"
$sourceSpnAppId = "GUID"
$requiredResourceAccess = (New-GraphGetRequest -tenantid $sourceTenantId -uri "https://graph.microsoft.com/v1.0/applications/$($sourceSpnId)").requiredResourceAccess
$oauth2Perms = (New-GraphGetRequest -tenantid $targetTenantId -uri "https://graph.microsoft.com/v1.0/servicePrincipals(appId='$($sourceSpnAppId)')/oauth2PermissionGrants")
$spn = (New-GraphGetRequest -tenantid $targetTenantId -uri "https://graph.microsoft.com/v1.0/servicePrincipals(appId='$($sourceSpnAppId)')")
$appRoles = (New-GraphGetRequest -tenantid $targetTenantId -uri "https://graph.microsoft.com/v1.0/servicePrincipals(appId='$($sourceSpnAppId)')/appRoleAssignments")
foreach($resource in $requiredResourceAccess){
$scopes = $Null; $scopes = $resource.resourceAccess | where{$_.type -eq "Scope"}
$roles = $Null; $roles = $resource.resourceAccess | where{$_.type -eq "Role"}
$targetSpn = (New-GraphGetRequest -tenantid $targetTenantId -uri "https://graph.microsoft.com/v1.0/servicePrincipals(appId='$($resource.resourceAppId)')")
#OAUTH2 (delegated) permissions
if($scopes){
$scopeArray = $Null;
foreach($scope in $scopes){
$scopeArray += "$(($targetSpn.oauth2PermissionScopes | Where-Object {$_.id -eq $scope.id}).value) "
}
$existingPermission = $oauth2Perms | Where-Object {$_.resourceId -eq $targetSpn.id -and $_.clientId -eq $spn.id}
$body = @{
"clientId"= $spn.id
"consentType"= "AllPrincipals"
"resourceId" = $targetSpn.id
"scope" = $scopeArray.Trim()
}
if($existingPermission){
$method = "PATCH"
$uri = "https://graph.microsoft.com/v1.0/oauth2PermissionGrants/$($existingPermission.id)"
}else{
$method = "POST"
$uri = "https://graph.microsoft.com/v1.0/oauth2PermissionGrants"
}
New-GraphPOSTRequest -type $method -tenantid $targetTenantId -uri $uri -body ($body | convertto-json -depth 15)
}
#Roles
if($roles){
foreach($role in $roles){
$existingPermission = $Null; $existingPermission = $appRoles | Where-Object {$_.appRoleId -eq $role.id -and $_.principalId -eq $spn.id -and $_.resourceId -eq $targetSpn.id}
if(!$existingPermission){
$body = @{
"principalId" = $spn.id
"resourceId" = $targetSpn.id
"appRoleId"= $role.id
}
New-GraphPOSTRequest -tenantid $targetTenantId -uri "https://graph.microsoft.com/v1.0/servicePrincipals(appId='$($resource.resourceAppId)')/appRoleAssignments" -body ($body | convertto-json -depth 15)
}
}
}
}