11
22
3- function New-ExoBulkRequest ( $tenantid , $cmdletArray , $useSystemMailbox , $Anchor , $NoAuthCheck , $Select ) {
3+ function New-ExoBulkRequest {
44 <#
55 . FUNCTIONALITY
66 Internal
77 #>
8+ [CmdletBinding ()]
9+ param (
10+ $tenantid ,
11+ $cmdletArray ,
12+ $useSystemMailbox ,
13+ $Anchor ,
14+ $NoAuthCheck ,
15+ $Select ,
16+ [switch ]$Compliance ,
17+ [switch ]$AsApp
18+ )
819 if ((Get-AuthorisedRequest - TenantID $tenantid ) -or $NoAuthCheck -eq $True ) {
9- $token = Get-ClassicAPIToken - resource ' https://outlook.office365.com' - Tenantid $tenantid
20+ if ($Compliance.IsPresent ) {
21+ $Resource = ' https://ps.compliance.protection.outlook.com'
22+ } else {
23+ $Resource = ' https://outlook.office365.com'
24+ }
25+ $Token = Get-GraphToken - Tenantid $tenantid - scope " $Resource /.default" - AsApp:$AsApp.IsPresent
26+
1027 $Tenant = Get-Tenants - IncludeErrors | Where-Object { $_.defaultDomainName -eq $tenantid -or $_.customerId -eq $tenantid }
1128 $Headers = @ {
12- Authorization = " Bearer $ ( $token .access_token ) "
29+ Authorization = $Token .Authorization
1330 Prefer = ' odata.maxpagesize = 1000;odata.continue-on-error'
1431 ' parameter-based-routing' = $true
1532 ' X-AnchorMailbox' = $Anchor
1633 }
34+
35+ if ($Compliance.IsPresent ) {
36+ if (! $Anchor ) {
37+ if (! $Tenant.initialDomainName -or $Tenant.initialDomainName -notlike ' *onmicrosoft.com*' ) {
38+ $OnMicrosoft = (New-GraphGetRequest - uri ' https://graph.microsoft.com/beta/domains?$top=999' - tenantid $tenantid - NoAuthCheck $NoAuthCheck | Where-Object - Property isInitial -EQ $true ).id
39+ } else {
40+ $OnMicrosoft = $Tenant.initialDomainName
41+ }
42+ $Headers.Anchor = " UPN:SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@$ ( $OnMicrosoft ) "
43+ }
44+ if (! $Tenant.ComplianceUrl ) {
45+ Write-Verbose " Getting Compliance URL for $ ( $tenant.defaultDomainName ) "
46+ $URL = " $Resource /adminapi/$ApiVersion /$ ( $tenant.customerId ) /EXOBanner('AutogenSession')?Version=$ModuleVersion "
47+ Invoke-RestMethod - ResponseHeadersVariable ComplianceHeaders - MaximumRedirection 0 - ErrorAction SilentlyContinue - Uri $URL - Headers $Headers - SkipHttpErrorCheck | Out-Null
48+ $RedirectedHost = ([System.Uri ]($ComplianceHeaders.Location | Select-Object - First 1 )).Host
49+ $RedirectedHostname = ' {0}.ps.compliance.protection.outlook.com' -f ($RedirectedHost -split ' \.' | Select-Object - First 1 )
50+ $Resource = " https://$ ( $RedirectedHostname ) "
51+ try {
52+ $null = [System.Uri ]$Resource
53+ $Tenant | Add-Member - MemberType NoteProperty - Name ComplianceUrl - Value $Resource
54+ $TenantTable = Get-CIPPTable - tablename ' Tenants'
55+ Add-CIPPAzDataTableEntity @TenantTable - Entity $Tenant - Force
56+ } catch {
57+ Write-Error " Failed to get the Compliance URL for $ ( $tenant.defaultDomainName ) , invalid URL - check the Anchor and try again."
58+ return
59+ }
60+ } else {
61+ $Resource = $Tenant.ComplianceUrl
62+ }
63+ Write-Verbose " Redirecting to $Resource "
64+ }
65+
1766 try {
1867 if ($Select ) { $Select = " `$ select=$Select " }
19- $URL = " https://outlook.office365.com /adminapi/beta/$ ( $tenant.customerId ) /InvokeCommand?$Select "
20- $BatchURL = " https://outlook.office365.com /adminapi/beta/$ ( $tenant.customerId ) /`$ batch"
68+ $URL = " $ResourceUrl /adminapi/beta/$ ( $tenant.customerId ) /InvokeCommand?$Select "
69+ $BatchURL = " $ResourceUrl /adminapi/beta/$ ( $tenant.customerId ) /`$ batch"
2170 $BatchBodyObj = @ {
2271 requests = @ ()
2372 }
@@ -84,4 +133,4 @@ function New-ExoBulkRequest ($tenantid, $cmdletArray, $useSystemMailbox, $Anchor
84133 } else {
85134 Write-Error ' Not allowed. You cannot manage your own tenant or tenants not under your scope'
86135 }
87- }
136+ }
0 commit comments