@@ -26,6 +26,7 @@ function Set-CIPPDBCacheMailboxes {
2626 Write-LogMessage - API ' CIPPDBCache' - tenant $TenantFilter - message ' Caching mailboxes' - sev Debug
2727
2828 # Get mailboxes and user details in a single bulk request
29+ $ZeroArchiveGuid = ' 00000000-0000-0000-0000-000000000000'
2930 $Select = ' id,ExchangeGuid,ArchiveGuid,UserPrincipalName,DisplayName,PrimarySMTPAddress,RecipientType,RecipientTypeDetails,EmailAddresses,WhenSoftDeleted,IsInactiveMailbox,ForwardingSmtpAddress,DeliverToMailboxAndForward,ForwardingAddress,HiddenFromAddressListsEnabled,ExternalDirectoryObjectId,MessageCopyForSendOnBehalfEnabled,MessageCopyForSentAsEnabled,GrantSendOnBehalfTo,PersistedCapabilities,LitigationHoldEnabled,LitigationHoldDate,LitigationHoldDuration,ComplianceTagHoldApplied,RetentionHoldEnabled,InPlaceHolds,RetentionPolicy,RemotePowerShellEnabled,Guid,Identity'
3031 $BulkRequests = @ (
3132 @ { CmdletInput = @ { CmdletName = ' Get-Mailbox' ; Parameters = @ {} } }
@@ -49,6 +50,12 @@ function Set-CIPPDBCacheMailboxes {
4950 @ { Name = ' UPN' ; Expression = { $_ .' UserPrincipalName' } },
5051 @ { Name = ' displayName' ; Expression = { $_ .' DisplayName' } },
5152 @ { Name = ' primarySmtpAddress' ; Expression = { $_ .' PrimarySMTPAddress' } },
53+ @ { Name = ' ArchiveEnabled' ; Expression = { $_.ArchiveGuid -and $_.ArchiveGuid.ToString () -ne $ZeroArchiveGuid } },
54+ @ { Name = ' ArchiveSize' ; Expression = { 0 } },
55+ @ { Name = ' ArchiveItemCount' ; Expression = { 0 } },
56+ @ { Name = ' storageUsedInBytes' ; Expression = { 0 } },
57+ @ { Name = ' prohibitSendReceiveQuotaInBytes' ; Expression = { 0 } },
58+ @ { Name = ' MailboxItemCount' ; Expression = { 0 } },
5259 @ { Name = ' recipientType' ; Expression = { $_ .' RecipientType' } },
5360 @ { Name = ' recipientTypeDetails' ; Expression = { $_ .' RecipientTypeDetails' } },
5461 @ { Name = ' AdditionalEmailAddresses' ; Expression = { ($_ .' EmailAddresses' | Where-Object { $_ -clike ' smtp:*' }).Replace(' smtp:' , ' ' ) -join ' , ' } },
@@ -73,6 +80,61 @@ function Set-CIPPDBCacheMailboxes {
7380 @ { Name = ' Identity' ; Expression = { $MatchedUser.Identity } }))
7481 }
7582
83+ # $MailboxByUPN is the only lookup that stores mailbox objects. Enrichment steps below
84+ # resolve back through this lookup before updating the objects written by Add-CIPPDbItem.
85+ $MailboxByUPN = @ {}
86+ foreach ($Mailbox in @ ($Mailboxes )) {
87+ if ($Mailbox.UPN ) {
88+ $MailboxByUPN [$Mailbox.UPN ] = $Mailbox
89+ }
90+ }
91+
92+ try {
93+ $MailboxUsage = New-GraphGetRequest - uri " https://graph.microsoft.com/beta/reports/getMailboxUsageDetail(period='D7')?`$ format=application%2fjson" - tenantid $TenantFilter
94+ foreach ($Usage in @ ($MailboxUsage )) {
95+ if ($Usage.userPrincipalName -and $MailboxByUPN.ContainsKey ($Usage.userPrincipalName )) {
96+ $Mailbox = $MailboxByUPN [$Usage.userPrincipalName ]
97+ $Mailbox.storageUsedInBytes = try { [int64 ]$Usage.storageUsedInBytes } catch { 0 }
98+ $Mailbox.prohibitSendReceiveQuotaInBytes = try { [int64 ]$Usage.prohibitSendReceiveQuotaInBytes } catch { 0 }
99+ $Mailbox.MailboxItemCount = try { [int64 ]$Usage.itemCount } catch { 0 }
100+ }
101+ }
102+ } catch {
103+ Write-LogMessage - API ' CIPPDBCache' - tenant $TenantFilter - message " Failed to cache mailbox usage details: $ ( $_.Exception.Message ) " - sev Warning
104+ }
105+
106+ $ArchiveMailboxes = @ ($Mailboxes | Where-Object { $_.ArchiveEnabled -eq $true -and $_.UPN })
107+ if ($ArchiveMailboxes.Count -gt 0 ) {
108+ Write-LogMessage - API ' CIPPDBCache' - tenant $TenantFilter - message " Caching archive statistics for $ ( $ArchiveMailboxes.Count ) mailboxes" - sev Debug
109+
110+ $MailboxUPNByArchiveStatsRequestId = @ {}
111+ $ArchiveStatsRequests = @ (foreach ($Mailbox in $ArchiveMailboxes ) {
112+ $OperationGuid = [Guid ]::NewGuid().ToString()
113+ $MailboxUPNByArchiveStatsRequestId [$OperationGuid ] = $Mailbox.UPN
114+
115+ @ {
116+ CmdletInput = @ {
117+ CmdletName = ' Get-MailboxStatistics'
118+ Parameters = @ {
119+ Identity = $Mailbox.UPN
120+ Archive = $true
121+ }
122+ }
123+ OperationGuid = $OperationGuid
124+ }
125+ })
126+
127+ $ArchiveStatsResults = New-ExoBulkRequest - tenantid $TenantFilter - cmdletArray $ArchiveStatsRequests - useSystemMailbox $true
128+ foreach ($ArchiveStat in @ ($ArchiveStatsResults )) {
129+ if ($ArchiveStat.OperationGuid -and $MailboxUPNByArchiveStatsRequestId.ContainsKey ($ArchiveStat.OperationGuid ) -and -not $ArchiveStat.error ) {
130+ $ArchiveMailboxUPN = $MailboxUPNByArchiveStatsRequestId [$ArchiveStat.OperationGuid ]
131+ $ArchiveMailbox = $MailboxByUPN [$ArchiveMailboxUPN ]
132+ $ArchiveMailbox.ArchiveSize = try { Get-ExoOnlineStringBytes - SizeString $ArchiveStat.TotalItemSize } catch { 0 }
133+ $ArchiveMailbox.ArchiveItemCount = try { [int64 ]$ArchiveStat.ItemCount } catch { 0 }
134+ }
135+ }
136+ }
137+
76138 $Mailboxes | Add-CIPPDbItem - TenantFilter $TenantFilter - Type ' Mailboxes' - AddCount
77139
78140 Write-LogMessage - API ' CIPPDBCache' - tenant $TenantFilter - message " Cached $ ( $Mailboxes.Count ) mailboxes successfully" - sev Debug
0 commit comments