Skip to content

Commit dea36c2

Browse files
authored
Merge pull request KelvinTegelaar#1100 from JohnDuprey/dev
ListExoRequest and ExoBulk changes
2 parents 7d208cd + 4f288ba commit dea36c2

2 files changed

Lines changed: 65 additions & 18 deletions

File tree

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Email-Exchange/Invoke-ListExoRequest.ps1

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ function Invoke-ListExoRequest {
77
'Search'
88
)
99

10-
Write-Information ($Request.Query | ConvertTo-Json)
11-
$Cmdlet = $Request.Query.Cmdlet
12-
$cmdParams = if ($Request.Body) { $Request.Body } else { [PSCustomObject]@{} }
10+
$Cmdlet = $Request.Body.Cmdlet
11+
$cmdParams = if ($Request.Body.cmdParams) { $Request.Body.cmdParams } else { [PSCustomObject]@{} }
1312
$Verb = ($Cmdlet -split '-')[0]
1413

1514
$AllowedTenants = Test-CIPPAccess -Request $Request -TenantList
16-
$TenantFilter = $Request.Query.TenantFilter
15+
$TenantFilter = $Request.Body.TenantFilter
1716
$Tenants = Get-Tenants -IncludeErrors
1817
$Tenant = $Tenants | Where-Object { $_.defaultDomainName -eq $TenantFilter -or $_.customerId -eq $TenantFilter }
1918
if ($Tenant.customerId -in $AllowedTenants -or $AllowedTenants -eq 'AllTenants') {
@@ -33,27 +32,26 @@ function Invoke-ListExoRequest {
3332
tenantid = $TenantFilter
3433
}
3534

36-
if ($Request.Query.Select) {
37-
$ExoParams.Select = $Request.Query.Select
35+
if ($Request.Body.Select) {
36+
$ExoParams.Select = $Request.Body.Select
3837
}
3938

40-
if ($Request.Query.UseSystemMailbox) {
39+
if ($Request.Body.UseSystemMailbox -eq $true) {
4140
$ExoParams.useSystemMailbox = $true
4241
}
4342

44-
if ($Request.Query.Anchor) {
45-
$ExoParams.Anchor = $Request.Query.Anchor
43+
if ($Request.Body.Anchor) {
44+
$ExoParams.Anchor = $Request.Body.Anchor
4645
}
4746

48-
if ($Request.Query.Compliance) {
47+
if ($Request.Body.Compliance -eq $true) {
4948
$ExoParams.Compliance = $true
5049
}
5150

52-
if ($Request.Query.AsApp) {
51+
if ($Request.Body.AsApp -eq $true) {
5352
$ExoParams.AsApp = $true
5453
}
5554

56-
Write-Information ($ExoParams | ConvertTo-Json)
5755
$Results = New-ExoRequest @ExoParams
5856
$Body = [pscustomobject]@{
5957
Results = $Results

Modules/CIPPCore/Public/GraphHelper/New-ExoBulkRequest.ps1

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,72 @@
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

Comments
 (0)