Skip to content

Commit b2a4599

Browse files
committed
Add mailboxes report API and use-report flag
Introduce Get-CIPPMailboxesReport to retrieve mailbox records from the reporting DB (supports TenantFilter and 'AllTenants' aggregation, attaches a CacheTimestamp, sorts by displayName and logs errors). Update Invoke-ListMailboxes to accept a UseReportDB query parameter; when set to 'true' it calls the new report function and returns the results (with HTTP status/error handling), otherwise it continues to use the existing live EXO logic.
1 parent 829afc9 commit b2a4599

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,27 @@ function Invoke-ListMailboxes {
99
param($Request, $TriggerMetadata)
1010
# Interact with query parameters or the body of the request.
1111
$TenantFilter = $Request.Query.tenantFilter
12+
$UseReportDB = $Request.Query.UseReportDB
13+
1214
try {
15+
# If UseReportDB is specified, retrieve from report database
16+
if ($UseReportDB -eq 'true') {
17+
try {
18+
$GraphRequest = Get-CIPPMailboxesReport -TenantFilter $TenantFilter -ErrorAction Stop
19+
$StatusCode = [HttpStatusCode]::OK
20+
} catch {
21+
Write-Host "Error retrieving mailboxes from report database: $($_.Exception.Message)"
22+
$StatusCode = [HttpStatusCode]::InternalServerError
23+
$GraphRequest = $_.Exception.Message
24+
}
25+
26+
return ([HttpResponseContext]@{
27+
StatusCode = $StatusCode
28+
Body = @($GraphRequest)
29+
})
30+
}
31+
32+
# Original live EXO logic
1333
$Select = 'id,ExchangeGuid,ArchiveGuid,UserPrincipalName,DisplayName,PrimarySMTPAddress,RecipientType,RecipientTypeDetails,EmailAddresses,WhenSoftDeleted,IsInactiveMailbox,ForwardingSmtpAddress,DeliverToMailboxAndForward,ForwardingAddress,HiddenFromAddressListsEnabled,ExternalDirectoryObjectId,MessageCopyForSendOnBehalfEnabled,MessageCopyForSentAsEnabled,PersistedCapabilities,LitigationHoldEnabled,LitigationHoldDate,LitigationHoldDuration,ComplianceTagHoldApplied,RetentionHoldEnabled,InPlaceHolds,RetentionPolicy'
1434
$ExoRequest = @{
1535
tenantid = $TenantFilter
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
function Get-CIPPMailboxesReport {
2+
<#
3+
.SYNOPSIS
4+
Generates a mailboxes report from the CIPP Reporting database
5+
6+
.DESCRIPTION
7+
Retrieves mailbox data for a tenant from the reporting database
8+
9+
.PARAMETER TenantFilter
10+
The tenant to generate the report for
11+
12+
.EXAMPLE
13+
Get-CIPPMailboxesReport -TenantFilter 'contoso.onmicrosoft.com'
14+
Gets all mailboxes for the tenant from the report database
15+
#>
16+
[CmdletBinding()]
17+
param(
18+
[Parameter(Mandatory = $true)]
19+
[string]$TenantFilter
20+
)
21+
22+
try {
23+
# Handle AllTenants
24+
if ($TenantFilter -eq 'AllTenants') {
25+
# Get all tenants that have mailbox data
26+
$AllMailboxItems = Get-CIPPDbItem -TenantFilter 'allTenants' -Type 'Mailboxes'
27+
$Tenants = @($AllMailboxItems | Where-Object { $_.RowKey -ne 'Mailboxes-Count' } | Select-Object -ExpandProperty PartitionKey -Unique)
28+
29+
$TenantList = Get-Tenants -IncludeErrors
30+
$Tenants = $Tenants | Where-Object { $TenantList.defaultDomainName -contains $_ }
31+
32+
$AllResults = [System.Collections.Generic.List[PSCustomObject]]::new()
33+
foreach ($Tenant in $Tenants) {
34+
try {
35+
$TenantResults = Get-CIPPMailboxesReport -TenantFilter $Tenant
36+
foreach ($Result in $TenantResults) {
37+
# Add Tenant property to each result
38+
$Result | Add-Member -NotePropertyName 'Tenant' -NotePropertyValue $Tenant -Force
39+
$AllResults.Add($Result)
40+
}
41+
} catch {
42+
Write-LogMessage -API 'MailboxesReport' -tenant $Tenant -message "Failed to get report for tenant: $($_.Exception.Message)" -sev Warning
43+
}
44+
}
45+
return $AllResults
46+
}
47+
48+
# Get mailboxes from reporting DB
49+
$MailboxItems = Get-CIPPDbItem -TenantFilter $TenantFilter -Type 'Mailboxes' | Where-Object { $_.RowKey -ne 'Mailboxes-Count' }
50+
if (-not $MailboxItems) {
51+
throw 'No mailbox data found in reporting database. Sync the report data first.'
52+
}
53+
54+
# Get the most recent cache timestamp
55+
$CacheTimestamp = ($MailboxItems | Where-Object { $_.Timestamp } | Sort-Object Timestamp -Descending | Select-Object -First 1).Timestamp
56+
57+
# Parse mailbox data
58+
$AllMailboxes = [System.Collections.Generic.List[PSCustomObject]]::new()
59+
foreach ($Item in $MailboxItems | Where-Object { $_.RowKey -ne 'Mailboxes-Count' }) {
60+
$Mailbox = $Item.Data | ConvertFrom-Json
61+
62+
# Add cache timestamp
63+
$Mailbox | Add-Member -NotePropertyName 'CacheTimestamp' -NotePropertyValue $CacheTimestamp -Force
64+
65+
$AllMailboxes.Add($Mailbox)
66+
}
67+
68+
return $AllMailboxes | Sort-Object -Property displayName
69+
70+
} catch {
71+
Write-LogMessage -API 'MailboxesReport' -tenant $TenantFilter -message "Failed to generate mailboxes report: $($_.Exception.Message)" -sev Error
72+
throw
73+
}
74+
}

0 commit comments

Comments
 (0)