|
| 1 | +function Get-CIPPMailboxForwardingReport { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Generates a mailbox forwarding report from the CIPP Reporting database |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Retrieves mailboxes that have forwarding configured (external, internal, or both) |
| 8 | + from the cached mailbox data. |
| 9 | +
|
| 10 | + .PARAMETER TenantFilter |
| 11 | + The tenant to generate the report for |
| 12 | +
|
| 13 | + .EXAMPLE |
| 14 | + Get-CIPPMailboxForwardingReport -TenantFilter 'contoso.onmicrosoft.com' |
| 15 | + Gets all mailboxes with forwarding configured |
| 16 | + #> |
| 17 | + [CmdletBinding()] |
| 18 | + param( |
| 19 | + [Parameter(Mandatory = $true)] |
| 20 | + [string]$TenantFilter |
| 21 | + ) |
| 22 | + |
| 23 | + try { |
| 24 | + Write-LogMessage -API 'MailboxForwardingReport' -tenant $TenantFilter -message 'Generating mailbox forwarding report' -sev Debug |
| 25 | + |
| 26 | + # Handle AllTenants |
| 27 | + if ($TenantFilter -eq 'AllTenants') { |
| 28 | + # Get all tenants that have mailbox data |
| 29 | + $AllMailboxItems = Get-CIPPDbItem -TenantFilter 'allTenants' -Type 'Mailboxes' |
| 30 | + $Tenants = @($AllMailboxItems | Where-Object { $_.RowKey -ne 'Mailboxes-Count' } | Select-Object -ExpandProperty PartitionKey -Unique) |
| 31 | + |
| 32 | + $TenantList = Get-Tenants -IncludeErrors |
| 33 | + $Tenants = $Tenants | Where-Object { $TenantList.defaultDomainName -contains $_ } |
| 34 | + |
| 35 | + $AllResults = [System.Collections.Generic.List[PSCustomObject]]::new() |
| 36 | + foreach ($Tenant in $Tenants) { |
| 37 | + try { |
| 38 | + $TenantResults = Get-CIPPMailboxForwardingReport -TenantFilter $Tenant |
| 39 | + foreach ($Result in $TenantResults) { |
| 40 | + $Result | Add-Member -NotePropertyName 'Tenant' -NotePropertyValue $Tenant -Force |
| 41 | + $AllResults.Add($Result) |
| 42 | + } |
| 43 | + } catch { |
| 44 | + Write-LogMessage -API 'MailboxForwardingReport' -tenant $Tenant -message "Failed to get report for tenant: $($_.Exception.Message)" -sev Warning |
| 45 | + } |
| 46 | + } |
| 47 | + return $AllResults |
| 48 | + } |
| 49 | + |
| 50 | + # Get mailboxes from reporting DB |
| 51 | + $MailboxItems = Get-CIPPDbItem -TenantFilter $TenantFilter -Type 'Mailboxes' | Where-Object { $_.RowKey -ne 'Mailboxes-Count' } |
| 52 | + if (-not $MailboxItems) { |
| 53 | + throw 'No mailbox data found in reporting database. Sync the mailbox data first.' |
| 54 | + } |
| 55 | + |
| 56 | + # Get the most recent cache timestamp |
| 57 | + $CacheTimestamp = ($MailboxItems | Where-Object { $_.Timestamp } | Sort-Object Timestamp -Descending | Select-Object -First 1).Timestamp |
| 58 | + |
| 59 | + # Parse mailbox data and build report |
| 60 | + $Report = [System.Collections.Generic.List[PSCustomObject]]::new() |
| 61 | + foreach ($Item in $MailboxItems) { |
| 62 | + $Mailbox = $Item.Data | ConvertFrom-Json |
| 63 | + |
| 64 | + # Determine forwarding status |
| 65 | + $HasExternalForwarding = -not [string]::IsNullOrWhiteSpace($Mailbox.ForwardingSmtpAddress) |
| 66 | + $HasInternalForwarding = -not [string]::IsNullOrWhiteSpace($Mailbox.InternalForwardingAddress) |
| 67 | + $HasAnyForwarding = $HasExternalForwarding -or $HasInternalForwarding |
| 68 | + |
| 69 | + # Only include mailboxes with forwarding configured |
| 70 | + if (-not $HasAnyForwarding) { |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + # Determine forwarding type for display (external takes precedence) |
| 75 | + $ForwardingType = if ($HasExternalForwarding) { |
| 76 | + 'External' |
| 77 | + } else { |
| 78 | + 'Internal' |
| 79 | + } |
| 80 | + |
| 81 | + # Build the forward-to address display (external takes precedence) |
| 82 | + $ForwardTo = if ($HasExternalForwarding) { |
| 83 | + $Mailbox.ForwardingSmtpAddress |
| 84 | + } else { |
| 85 | + $Mailbox.InternalForwardingAddress |
| 86 | + } |
| 87 | + |
| 88 | + $Report.Add([PSCustomObject]@{ |
| 89 | + UPN = $Mailbox.UPN |
| 90 | + DisplayName = $Mailbox.displayName |
| 91 | + PrimarySmtpAddress = $Mailbox.primarySmtpAddress |
| 92 | + RecipientTypeDetails = $Mailbox.recipientTypeDetails |
| 93 | + ForwardingType = $ForwardingType |
| 94 | + ForwardTo = $ForwardTo |
| 95 | + ForwardingSmtpAddress = $Mailbox.ForwardingSmtpAddress |
| 96 | + InternalForwardingAddress = $Mailbox.InternalForwardingAddress |
| 97 | + DeliverToMailboxAndForward = $Mailbox.DeliverToMailboxAndForward |
| 98 | + Tenant = $TenantFilter |
| 99 | + CacheTimestamp = $CacheTimestamp |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + # Sort by display name |
| 104 | + $Report = $Report | Sort-Object -Property DisplayName |
| 105 | + |
| 106 | + Write-LogMessage -API 'MailboxForwardingReport' -tenant $TenantFilter -message "Generated forwarding report with $($Report.Count) entries" -sev Debug |
| 107 | + return $Report |
| 108 | + |
| 109 | + } catch { |
| 110 | + Write-LogMessage -API 'MailboxForwardingReport' -tenant $TenantFilter -message "Failed to generate mailbox forwarding report: $($_.Exception.Message)" -sev Error -LogData (Get-CippException -Exception $_) |
| 111 | + throw "Failed to generate mailbox forwarding report: $($_.Exception.Message)" |
| 112 | + } |
| 113 | +} |
0 commit comments