@@ -12,15 +12,15 @@ Function Invoke-ExecOffboardTenant {
12
12
try {
13
13
Write-LogMessage - user $request.headers .' x-ms-client-principal' - API $APINAME - message ' Accessed this API' - Sev ' Debug'
14
14
15
- $Tenantfilter = $request .body.tenantfilter
16
-
17
- # temp fix -rvdwegen
18
- $tenantId = ( Invoke-RestMethod - Method GET " https://login.windows.net/ $Tenantfilter /.well-known/openid-configuration " ).token_endpoint.Split( ' / ' )[ 3 ]
15
+ $TenantQuery = $Request .Body.TenantFilter.value ?? $Request .Body.TenantFilter
16
+ $Tenant = Get-Tenants - IncludeAll - TenantFilter $TenantQuery
17
+ $TenantId = $Tenant .customerId
18
+ $TenantFilter = $Tenant .defaultDomainName
19
19
20
20
$results = [System.Collections.ArrayList ]@ ()
21
21
$errors = [System.Collections.ArrayList ]@ ()
22
22
23
- if ($request.body.RemoveCSPGuestUsers ) {
23
+ if ($request.body.RemoveCSPGuestUsers -eq $true ) {
24
24
# Delete guest users who's domains match the CSP tenants
25
25
try {
26
26
try {
@@ -42,7 +42,7 @@ Function Invoke-ExecOffboardTenant {
42
42
$BulkResults = New-GraphBulkRequest - Requests $BulkRequests - tenantid $TenantFilter
43
43
44
44
$results.Add (' Successfully removed guest users' )
45
- Write-LogMessage - user $ExecutingUser - API $APIName - message " CSP Guest users were removed" - Sev " Info" - tenant $TenantFilter
45
+ Write-LogMessage - user $ExecutingUser - API $APIName - message ' CSP Guest users were removed' - Sev ' Info' - tenant $TenantFilter
46
46
} else {
47
47
$results.Add (' No guest users found to remove' )
48
48
}
@@ -51,7 +51,7 @@ Function Invoke-ExecOffboardTenant {
51
51
}
52
52
}
53
53
54
- if ($request.body.RemoveCSPnotificationContacts ) {
54
+ if ($request.body.RemoveCSPnotificationContacts -eq $true ) {
55
55
# Remove all email adresses that match the CSP tenants domains from the contact properties in /organization
56
56
try {
57
57
try {
@@ -72,19 +72,19 @@ Function Invoke-ExecOffboardTenant {
72
72
}
73
73
74
74
# foreach through the properties we want to check/update
75
- @ (' marketingNotificationEmails' , ' securityComplianceNotificationMails' , ' technicalNotificationMails' ) | ForEach-Object {
75
+ @ (' marketingNotificationEmails' , ' securityComplianceNotificationMails' , ' technicalNotificationMails' ) | ForEach-Object {
76
76
$property = $_
77
77
$propertyContacts = $orgContacts .($ ($property ))
78
78
79
- if ($propertyContacts -AND ($domains -notcontains ($propertyContacts | ForEach-Object { $_.Split (" @ " )[1 ] }))) {
80
- $newPropertyContent = [System.Collections.Generic.List [object ]]($propertyContacts | Where-Object { $domains -notcontains $_.Split (" @ " )[1 ] })
79
+ if ($propertyContacts -AND ($domains -notcontains ($propertyContacts | ForEach-Object { $_.Split (' @ ' )[1 ] }))) {
80
+ $newPropertyContent = [System.Collections.Generic.List [object ]]($propertyContacts | Where-Object { $domains -notcontains $_.Split (' @ ' )[1 ] })
81
81
82
82
$patchContactBody = if (! ($newPropertyContent )) { " { `" $ ( $property ) `" : [] }" } else { [pscustomobject ]@ { $property = $newPropertyContent } | ConvertTo-Json }
83
83
84
84
try {
85
- New-GraphPostRequest - type PATCH - body $patchContactBody - Uri " https://graph.microsoft.com/v1.0/organization/$ ( $orgContacts.id ) " - tenantid $Tenantfilter - ContentType " application/json"
86
- $results.Add (" Successfully removed notification contacts from $ ( $property ) : $ ( ($propertyContacts | Where-Object { $domains -contains $_.Split (" @ " )[1 ] })) " )
87
- Write-LogMessage - user $ExecutingUser - API $APIName - message " Contacts were removed from $ ( $property ) " - Sev " Info" - tenant $TenantFilter
85
+ New-GraphPostRequest - type PATCH - body $patchContactBody - Uri " https://graph.microsoft.com/v1.0/organization/$ ( $orgContacts.id ) " - tenantid $Tenantfilter - ContentType ' application/json'
86
+ $results.Add (" Successfully removed notification contacts from $ ( $property ) : $ ( ($propertyContacts | Where-Object { $domains -contains $_.Split (' @ ' )[1 ] })) " )
87
+ Write-LogMessage - user $ExecutingUser - API $APIName - message " Contacts were removed from $ ( $property ) " - Sev ' Info' - tenant $TenantFilter
88
88
} catch {
89
89
$errors.Add (" Failed to update property $ ( $property ) : $ ( $_.Exception.message ) " )
90
90
}
@@ -95,13 +95,13 @@ Function Invoke-ExecOffboardTenant {
95
95
# Add logic for privacyProfile later - rvdwegen
96
96
97
97
}
98
-
99
- if ($request .body.RemoveVendorApps ) {
100
- $request .body.RemoveVendorApps | ForEach-Object {
98
+ $VendorApps = $Request .Body.vendorApplications
99
+ if ($VendorApps ) {
100
+ $VendorApps | ForEach-Object {
101
101
try {
102
102
$delete = (New-GraphPostRequest - type ' DELETE' - Uri " https://graph.microsoft.com/v1.0/serviceprincipals/$ ( $_.value ) " - tenantid $Tenantfilter )
103
103
$results.Add (" Successfully removed app $ ( $_.label ) " )
104
- Write-LogMessage - user $ExecutingUser - API $APIName - message " App $ ( $_.label ) was removed" - Sev " Info" - tenant $TenantFilter
104
+ Write-LogMessage - user $ExecutingUser - API $APIName - message " App $ ( $_.label ) was removed" - Sev ' Info' - tenant $TenantFilter
105
105
} catch {
106
106
# $results.Add("Failed to removed app $($_.displayName)")
107
107
$errors.Add (" Failed to removed app $ ( $_.label ) " )
@@ -110,7 +110,7 @@ Function Invoke-ExecOffboardTenant {
110
110
}
111
111
112
112
# All customer tenant specific actions ALWAYS have to be completed before this action!
113
- if ($request.body.RemoveMultitenantCSPApps ) {
113
+ if ($request.body.RemoveMultitenantCSPApps -eq $true ) {
114
114
# Remove multi-tenant apps with the CSP tenant as origin
115
115
try {
116
116
$multitenantCSPApps = (New-GraphGETRequest - Uri " https://graph.microsoft.com/v1.0/servicePrincipals?`$ count=true&`$ select=displayName,appId,id,appOwnerOrganizationId&`$ filter=appOwnerOrganizationId eq $ ( $env: TenantID ) " - tenantid $Tenantfilter - ComplexFilter)
@@ -119,7 +119,7 @@ Function Invoke-ExecOffboardTenant {
119
119
try {
120
120
$delete = (New-GraphPostRequest - type ' DELETE' - Uri " https://graph.microsoft.com/v1.0/serviceprincipals/$ ( $_.id ) " - tenantid $Tenantfilter )
121
121
$results.Add (" Successfully removed app $ ( $_.displayName ) " )
122
- Write-LogMessage - user $ExecutingUser - API $APIName - message " App $ ( $_.displayName ) was removed" - Sev " Info" - tenant $TenantFilter
122
+ Write-LogMessage - user $ExecutingUser - API $APIName - message " App $ ( $_.displayName ) was removed" - Sev ' Info' - tenant $TenantFilter
123
123
} catch {
124
124
# $results.Add("Failed to removed app $($_.displayName)")
125
125
$errors.Add (" Failed to removed app $ ( $_.displayName ) " )
@@ -131,7 +131,7 @@ Function Invoke-ExecOffboardTenant {
131
131
}
132
132
}
133
133
134
- if ($request.body.TerminateGDAP ) {
134
+ if ($request.body.TerminateGDAP -eq $true ) {
135
135
# Terminate GDAP relationships
136
136
try {
137
137
$TenantFilter
@@ -142,7 +142,7 @@ Function Invoke-ExecOffboardTenant {
142
142
try {
143
143
$terminate = (New-GraphPostRequest - type ' POST' - Uri " https://graph.microsoft.com/v1.0/tenantRelationships/delegatedAdminRelationships/$ ( $_.id ) /requests" - body ' {"action":"terminate"}' - ContentType ' application/json' - tenantid $env: TenantID )
144
144
$results.Add (" Successfully terminated GDAP relationship $ ( $_.displayName ) from tenant $TenantFilter " )
145
- Write-LogMessage - user $ExecutingUser - API $APIName - message " GDAP Relationship $ ( $_.displayName ) has been terminated" - Sev " Info" - tenant $TenantFilter
145
+ Write-LogMessage - user $ExecutingUser - API $APIName - message " GDAP Relationship $ ( $_.displayName ) has been terminated" - Sev ' Info' - tenant $TenantFilter
146
146
} catch {
147
147
$ ($_.Exception.message )
148
148
# $results.Add("Failed to terminate GDAP relationship $($_.displayName): $($_.Exception.message)")
@@ -156,12 +156,12 @@ Function Invoke-ExecOffboardTenant {
156
156
}
157
157
}
158
158
159
- if ($request.body.TerminateContract ) {
159
+ if ($request.body.TerminateContract -eq $true ) {
160
160
# Terminate contract relationship
161
161
try {
162
162
$terminate = (New-GraphPostRequest - type ' PATCH' - body ' { "relationshipToPartner": "none" }' - Uri " https://api.partnercenter.microsoft.com/v1/customers/$TenantFilter " - ContentType ' application/json' - scope ' https://api.partnercenter.microsoft.com/user_impersonation' - tenantid $env: TenantID )
163
163
$results.Add (' Successfully terminated contract relationship' )
164
- Write-LogMessage - user $ExecutingUser - API $APIName - message " Contract relationship terminated" - Sev " Info" - tenant $TenantFilter
164
+ Write-LogMessage - user $ExecutingUser - API $APIName - message ' Contract relationship terminated' - Sev ' Info' - tenant $TenantFilter
165
165
} catch {
166
166
# $results.Add("Failed to terminate contract relationship: $($_.Exception.message)")
167
167
$errors.Add (" Failed to terminate contract relationship: $ ( $_.Exception.message ) " )
0 commit comments