@@ -5,7 +5,8 @@ function Add-CIPPDelegatedPermission {
55 $TemplateId ,
66 $ApplicationId ,
77 $NoTranslateRequired ,
8- $TenantFilter
8+ $TenantFilter ,
9+ [bool ]$AsApp = $false
910 )
1011 Write-Information ' Adding Delegated Permissions'
1112 $ApplicationId = $ApplicationId ?? $env: ApplicationID
@@ -71,19 +72,34 @@ function Add-CIPPDelegatedPermission {
7172 }
7273
7374 $Translator = Get-Content (Join-Path $env: CIPPRootPath ' Config\PermissionsTranslator.json' ) | ConvertFrom-Json
74- $ServicePrincipalList = New-GraphGETRequest - uri " https://graph.microsoft.com/beta/servicePrincipals?`$ select=appId,id,displayName&`$ top=999" - tenantid $TenantFilter - skipTokenCache $true - NoAuthCheck $true
75+
76+ # Use app auth when requested, but fall back to delegated on the first failure - the app may not
77+ # hold the application permissions needed. $UseAsApp latches to $false for the rest of the run.
78+ $UseAsApp = $AsApp
79+ $GetServicePrincipalList = { New-GraphGETRequest - uri " https://graph.microsoft.com/beta/servicePrincipals?`$ select=appId,id,displayName&`$ top=999" - tenantid $TenantFilter - skipTokenCache $true - NoAuthCheck $true - AsApp $UseAsApp }
80+ try {
81+ $ServicePrincipalList = & $GetServicePrincipalList
82+ } catch {
83+ if ($UseAsApp ) {
84+ Write-Information " App-auth request failed, falling back to delegated permissions: $ ( $_.Exception.Message ) "
85+ $UseAsApp = $false
86+ $ServicePrincipalList = & $GetServicePrincipalList
87+ } else {
88+ throw
89+ }
90+ }
7591 $Results = [System.Collections.Generic.List [string ]]::new()
7692
7793 $ourSVCPrincipal = $ServicePrincipalList | Where-Object - Property AppId -EQ $ApplicationId | Select-Object - First 1
7894 if (! $ourSVCPrincipal ) {
79- $ourSvcPrincipal = New-GraphGETRequest - uri " https://graph.microsoft.com/beta/servicePrincipals(appId='$ApplicationId ')?`$ select=appId,id,displayName" - tenantid $TenantFilter - skipTokenCache $true - NoAuthCheck $true
95+ $ourSvcPrincipal = New-GraphGETRequest - uri " https://graph.microsoft.com/beta/servicePrincipals(appId='$ApplicationId ')?`$ select=appId,id,displayName" - tenantid $TenantFilter - skipTokenCache $true - NoAuthCheck $true - AsApp $UseAsApp
8096 }
8197 if (! $ourSVCPrincipal ) {
8298 $Results.Add (" Failed to find service principal for application $ApplicationId in tenant $TenantFilter " )
8399 return $Results
84100 }
85101
86- $CurrentDelegatedScopes = New-GraphGETRequest - uri " https://graph.microsoft.com/beta/servicePrincipals/$ ( $ourSVCPrincipal.id ) /oauth2PermissionGrants" - skipTokenCache $true - tenantid $TenantFilter - NoAuthCheck $true
102+ $CurrentDelegatedScopes = New-GraphGETRequest - uri " https://graph.microsoft.com/beta/servicePrincipals/$ ( $ourSVCPrincipal.id ) /oauth2PermissionGrants" - skipTokenCache $true - tenantid $TenantFilter - NoAuthCheck $true - AsApp $UseAsApp
87103
88104 foreach ($App in $RequiredResourceAccess ) {
89105 if (! $App ) {
@@ -95,7 +111,7 @@ function Add-CIPPDelegatedPermission {
95111 $Body = @ {
96112 appId = $App.resourceAppId
97113 } | ConvertTo-Json - Compress
98- $svcPrincipalId = New-GraphPOSTRequest - uri ' https://graph.microsoft.com/v1.0/servicePrincipals' - tenantid $TenantFilter - body $Body - type POST - NoAuthCheck $true
114+ $svcPrincipalId = New-GraphPOSTRequest - uri ' https://graph.microsoft.com/v1.0/servicePrincipals' - tenantid $TenantFilter - body $Body - type POST - NoAuthCheck $true - AsApp $UseAsApp
99115 } catch {
100116 $Results.add (" Failed to create service principal for $ ( $App.resourceAppId ) : $ ( Get-NormalizedError - message $_.Exception.Message ) " )
101117 continue
@@ -134,7 +150,7 @@ function Add-CIPPDelegatedPermission {
134150 resourceId = $svcPrincipalId.id
135151 scope = $NewScope
136152 } | ConvertTo-Json - Compress
137- $CreateRequest = New-GraphPOSTRequest - uri ' https://graph.microsoft.com/v1.0/oauth2PermissionGrants' - tenantid $TenantFilter - body $Createbody - type POST - NoAuthCheck $true
153+ $CreateRequest = New-GraphPOSTRequest - uri ' https://graph.microsoft.com/v1.0/oauth2PermissionGrants' - tenantid $TenantFilter - body $Createbody - type POST - NoAuthCheck $true - AsApp $UseAsApp
138154 $Results.add (" Successfully added permissions for $ ( $svcPrincipalId.displayName ) " )
139155 } catch {
140156 $Results.add (" Failed to add permissions for $ ( $svcPrincipalId.displayName ) : $ ( Get-NormalizedError - message $_.Exception.Message ) " )
@@ -147,7 +163,7 @@ function Add-CIPPDelegatedPermission {
147163 $OldScope.id | ForEach-Object {
148164 if ($_ -ne $OldScopeId ) {
149165 try {
150- $null = New-GraphPOSTRequest - uri " https://graph.microsoft.com/v1.0/oauth2PermissionGrants/$_ " - tenantid $TenantFilter - type DELETE - NoAuthCheck $true
166+ $null = New-GraphPOSTRequest - uri " https://graph.microsoft.com/v1.0/oauth2PermissionGrants/$_ " - tenantid $TenantFilter - type DELETE - NoAuthCheck $true - AsApp $UseAsApp
151167 } catch {
152168 }
153169 }
@@ -171,7 +187,7 @@ function Add-CIPPDelegatedPermission {
171187 scope = " $NewScope "
172188 } | ConvertTo-Json - Compress
173189 try {
174- $null = New-GraphPOSTRequest - uri " https://graph.microsoft.com/v1.0/oauth2PermissionGrants/$ ( $OldScopeId ) " - tenantid $TenantFilter - body $Patchbody - type PATCH - NoAuthCheck $true
190+ $null = New-GraphPOSTRequest - uri " https://graph.microsoft.com/v1.0/oauth2PermissionGrants/$ ( $OldScopeId ) " - tenantid $TenantFilter - body $Patchbody - type PATCH - NoAuthCheck $true - AsApp $UseAsApp
175191 } catch {
176192 $Results.add (" Failed to update permissions for $ ( $svcPrincipalId.displayName ) : $ ( Get-NormalizedError - message $_.Exception.Message ) " )
177193 continue
0 commit comments