Skip to content

Commit 5be5429

Browse files
committed
offboarding tweak
1 parent fd0939f commit 5be5429

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Administration/Invoke-ExecOffboardTenant.ps1

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ Function Invoke-ExecOffboardTenant {
1212
try {
1313
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
1414

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
1919

2020
$results = [System.Collections.ArrayList]@()
2121
$errors = [System.Collections.ArrayList]@()
2222

23-
if ($request.body.RemoveCSPGuestUsers) {
23+
if ($request.body.RemoveCSPGuestUsers -eq $true) {
2424
# Delete guest users who's domains match the CSP tenants
2525
try {
2626
try {
@@ -42,7 +42,7 @@ Function Invoke-ExecOffboardTenant {
4242
$BulkResults = New-GraphBulkRequest -Requests $BulkRequests -tenantid $TenantFilter
4343

4444
$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
4646
} else {
4747
$results.Add('No guest users found to remove')
4848
}
@@ -51,7 +51,7 @@ Function Invoke-ExecOffboardTenant {
5151
}
5252
}
5353

54-
if ($request.body.RemoveCSPnotificationContacts) {
54+
if ($request.body.RemoveCSPnotificationContacts -eq $true) {
5555
# Remove all email adresses that match the CSP tenants domains from the contact properties in /organization
5656
try {
5757
try {
@@ -72,19 +72,19 @@ Function Invoke-ExecOffboardTenant {
7272
}
7373

7474
# foreach through the properties we want to check/update
75-
@('marketingNotificationEmails','securityComplianceNotificationMails','technicalNotificationMails') | ForEach-Object {
75+
@('marketingNotificationEmails', 'securityComplianceNotificationMails', 'technicalNotificationMails') | ForEach-Object {
7676
$property = $_
7777
$propertyContacts = $orgContacts.($($property))
7878

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] })
8181

8282
$patchContactBody = if (!($newPropertyContent)) { "{ `"$($property)`" : [] }" } else { [pscustomobject]@{ $property = $newPropertyContent } | ConvertTo-Json }
8383

8484
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
8888
} catch {
8989
$errors.Add("Failed to update property $($property): $($_.Exception.message)")
9090
}
@@ -95,13 +95,13 @@ Function Invoke-ExecOffboardTenant {
9595
# Add logic for privacyProfile later - rvdwegen
9696

9797
}
98-
99-
if ($request.body.RemoveVendorApps) {
100-
$request.body.RemoveVendorApps | ForEach-Object {
98+
$VendorApps = $Request.Body.vendorApplications
99+
if ($VendorApps) {
100+
$VendorApps | ForEach-Object {
101101
try {
102102
$delete = (New-GraphPostRequest -type 'DELETE' -Uri "https://graph.microsoft.com/v1.0/serviceprincipals/$($_.value)" -tenantid $Tenantfilter)
103103
$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
105105
} catch {
106106
#$results.Add("Failed to removed app $($_.displayName)")
107107
$errors.Add("Failed to removed app $($_.label)")
@@ -110,7 +110,7 @@ Function Invoke-ExecOffboardTenant {
110110
}
111111

112112
# 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) {
114114
# Remove multi-tenant apps with the CSP tenant as origin
115115
try {
116116
$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 {
119119
try {
120120
$delete = (New-GraphPostRequest -type 'DELETE' -Uri "https://graph.microsoft.com/v1.0/serviceprincipals/$($_.id)" -tenantid $Tenantfilter)
121121
$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
123123
} catch {
124124
#$results.Add("Failed to removed app $($_.displayName)")
125125
$errors.Add("Failed to removed app $($_.displayName)")
@@ -131,7 +131,7 @@ Function Invoke-ExecOffboardTenant {
131131
}
132132
}
133133

134-
if ($request.body.TerminateGDAP) {
134+
if ($request.body.TerminateGDAP -eq $true) {
135135
# Terminate GDAP relationships
136136
try {
137137
$TenantFilter
@@ -142,7 +142,7 @@ Function Invoke-ExecOffboardTenant {
142142
try {
143143
$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)
144144
$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
146146
} catch {
147147
$($_.Exception.message)
148148
#$results.Add("Failed to terminate GDAP relationship $($_.displayName): $($_.Exception.message)")
@@ -156,12 +156,12 @@ Function Invoke-ExecOffboardTenant {
156156
}
157157
}
158158

159-
if ($request.body.TerminateContract) {
159+
if ($request.body.TerminateContract -eq $true) {
160160
# Terminate contract relationship
161161
try {
162162
$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)
163163
$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
165165
} catch {
166166
#$results.Add("Failed to terminate contract relationship: $($_.Exception.message)")
167167
$errors.Add("Failed to terminate contract relationship: $($_.Exception.message)")

0 commit comments

Comments
 (0)