@@ -4,43 +4,74 @@ function New-CIPPApplicationCopy {
44 $App ,
55 $Tenant
66 )
7- $CurrentInfo = New-GraphGetRequest - Uri ' https://graph.microsoft.com/beta/servicePrincipals?$top=999' - tenantid $env: TenantID - NoAuthCheck $true
8- try {
9- $ExistingApp = New-GraphGETRequest - uri " https://graph.microsoft.com/beta/Applications(appId='$ ( $app ) ')" - tenantid $ENV: TenantID - NoAuthCheck $true
10- $Type = ' Application'
11- } catch {
12- $ExistingApp = New-GraphGETRequest - uri " https://graph.microsoft.com/beta/servicePrincipals(appId='$ ( $app ) ')/oauth2PermissionGrants" - tenantid $ENV: TenantID - NoAuthCheck $true
13- $ExistingAppRoleAssignments = New-GraphGETRequest - uri " https://graph.microsoft.com/beta/servicePrincipals(appId='$ ( $app ) ')/appRoleAssignments" - tenantid $ENV: TenantID - NoAuthCheck $true
14- $Type = ' ServicePrincipal'
15- }
16- if (! $ExistingApp ) {
17- Write-LogMessage - message " Failed to add $App to tenant. This app does not exist." - tenant $tenant - API ' Application Copy' - sev error
18- continue
7+
8+ Write-Information " Copying application $ ( $App ) to tenant $Tenant "
9+ $CurrentInfo = New-GraphGetRequest - Uri ' https://graph.microsoft.com/v1.0/servicePrincipals?$top=999' - tenantid $env: TenantID - NoAuthCheck $true - AsApp $true
10+
11+ if ($CurrentInfo.appId -notcontains $App ) {
12+ Write-Information " Application $ ( $App ) not found in partner tenant. Cannot copy permissions."
13+ throw ' We cannot copy permissions for this application because is not registered in the partner tenant.'
1914 }
20- if ($Type -eq ' Application' ) {
21- $DelegateResourceAccess = $Existingapp.requiredResourceAccess
22- $ApplicationResourceAccess = $Existingapp.requiredResourceAccess
23- $NoTranslateRequired = $false
24- } else {
25- $DelegateResourceAccess = $ExistingApp | Group-Object - Property resourceId | ForEach-Object {
26- [pscustomobject ]@ { resourceAppId = ($CurrentInfo | Where-Object - Property id -EQ $_.Name ).appId; resourceAccess = @ ($_.Group | ForEach-Object { [pscustomobject ]@ { id = $_.scope ; type = ' Scope' } } )
15+
16+ try {
17+ try {
18+ $ExistingApp = New-GraphGETRequest - uri " https://graph.microsoft.com/beta/applications(appId='$ ( $app ) ')" - tenantid $ENV: TenantID - NoAuthCheck $true - AsApp $true
19+ $Type = ' Application'
20+ } catch {
21+ $ExistingApp = New-GraphGETRequest - uri " https://graph.microsoft.com/beta/servicePrincipals(appId='$ ( $app ) ')/oauth2PermissionGrants" - tenantid $ENV: TenantID - NoAuthCheck $true - AsApp $true
22+ $ExistingAppRoleAssignments = New-GraphGETRequest - uri " https://graph.microsoft.com/beta/servicePrincipals(appId='$ ( $app ) ')/appRoleAssignments" - tenantid $ENV: TenantID - NoAuthCheck $true - AsApp $true
23+ $Type = ' ServicePrincipal'
24+ }
25+ if (! $ExistingApp ) {
26+ Write-LogMessage - message " Failed to add $App to tenant. This app does not exist." - tenant $tenant - API ' Application Copy' - sev error
27+ continue
28+ }
29+ if ($Type -eq ' Application' ) {
30+ Write-Information ' App type: Application'
31+ $DelegateResourceAccess = $Existingapp.requiredResourceAccess
32+ $ApplicationResourceAccess = $Existingapp.requiredResourceAccess
33+ $NoTranslateRequired = $false
34+ } else {
35+ Write-Information ' App type: ServicePrincipal'
36+ $DelegateResourceAccess = $ExistingApp | Group-Object - Property resourceId | ForEach-Object {
37+ [pscustomobject ]@ { resourceAppId = ($CurrentInfo | Where-Object - Property id -EQ $_.Name ).appId; resourceAccess = @ ($_.Group | ForEach-Object { [pscustomobject ]@ { id = $_.scope ; type = ' Scope' } } )
38+ }
2739 }
40+ $ApplicationResourceAccess = $ExistingappRoleAssignments | Group-Object - Property ResourceId | ForEach-Object {
41+ [pscustomobject ]@ { resourceAppId = ($CurrentInfo | Where-Object - Property id -EQ $_.Name ).appId; resourceAccess = @ ($_.Group | ForEach-Object { [pscustomobject ]@ { id = $_.appRoleId ; type = ' Role' } } )
42+ }
43+ }
44+ $NoTranslateRequired = $true
2845 }
29- $ApplicationResourceAccess = $ExistingappRoleAssignments | Group-Object - Property ResourceId | ForEach-Object {
30- [pscustomobject ]@ { resourceAppId = ($CurrentInfo | Where-Object - Property id -EQ $_.Name ).appId; resourceAccess = @ ($_.Group | ForEach-Object { [pscustomobject ]@ { id = $_.appRoleId ; type = ' Role' } } )
46+ $TenantInfo = New-GraphGetRequest - Uri ' https://graph.microsoft.com/beta/servicePrincipals?$top=999' - tenantid $Tenant - NoAuthCheck $true - AsApp $true
47+
48+ if ($App -Notin $TenantInfo.appId ) {
49+ Write-Information " Creating service principal with ID: $ ( $App ) "
50+ $Body = @ {
51+ appId = $App
3152 }
53+ $Body = $Body | ConvertTo-Json - Compress
54+ Write-Information ($Body | ConvertTo-Json - Depth 10 )
55+ $null = New-GraphPostRequest ' https://graph.microsoft.com/v1.0/servicePrincipals' - type POST - tenantid $Tenant - body $Body - AsApp $true
56+ Write-LogMessage - message " Added $App as a service principal" - tenant $tenant - API ' Application Copy' - sev Info
57+
58+ } else {
59+ Write-Information " Service principal with ID: $ ( $App ) already exists in tenant $Tenant "
3260 }
33- $NoTranslateRequired = $true
34- }
35- $TenantInfo = New-GraphGetRequest - Uri ' https://graph.microsoft.com/beta/servicePrincipals?$top=999' - tenantid $Tenant - NoAuthCheck $true
3661
37- if ($App -Notin $TenantInfo .appId ) {
38- $null = New-GraphPostRequest ' https://graph.microsoft.com/beta/servicePrincipals ' - type POST - tenantid $Tenant - body " { `" appId `" : `" $ ( $ App) `" } "
39- Write-LogMessage - message " Added $App as a service principal " - tenant $tenant - API ' Application Copy ' - sev Info
40- }
41- Add-CIPPApplicationPermission - RequiredResourceAccess $ApplicationResourceAccess - ApplicationId $App - Tenantfilter $Tenant
42- Add-CIPPDelegatedPermission - RequiredResourceAccess $DelegateResourceAccess - ApplicationId $App - Tenantfilter $Tenant - NoTranslateRequired $NoTranslateRequired
43- Write-LogMessage - message " Added permissions to $app " - tenant $tenant - API ' Application Copy' - sev Info
62+ if ($DelegateResourceAccess ) {
63+ Add-CIPPDelegatedPermission - RequiredResourceAccess $ApplicationResourceAccess - ApplicationId $ App - Tenantfilter $Tenant
64+ }
65+ if ( $ApplicationResourceAccess ) {
66+ Add-CIPPApplicationPermission - RequiredResourceAccess $ApplicationResourceAccess - ApplicationId $App - Tenantfilter $Tenant
67+ }
68+ Write-LogMessage - message " Added permissions to $app " - tenant $tenant - API ' Application Copy' - sev Info
4469
45- return $Results
70+ return $Results
71+ } catch {
72+ Write-Warning " Failed to copy application $ ( $App ) to tenant $Tenant . Error: $ ( $_.Exception.Message ) "
73+ Write-Information $_.InvocationInfo.PositionMessage
74+ Write-Information ($_.ScriptStackTrace | Out-String )
75+ throw $_.Exception.Message
76+ }
4677}
0 commit comments