<# .SYNOPSIS TenantPulse optional email deep-check (READ-ONLY). .DESCRIPTION Exports, to a local JSON file, the Exchange Online settings TenantPulse needs to look for mail forwarding and suspicious inbox rules: - mailboxes with ForwardingSmtpAddress / ForwardingAddress / DeliverToMailboxAndForward - inbox rules that forward, redirect, delete or move mail (ForwardTo, RedirectTo, ForwardAsAttachmentTo, DeleteMessage, MoveToFolder e.g. the "RSS Feeds" folder) - outbound spam policy AutoForwardingMode - remote domain AutoForwardEnabled This script only READS data. It does not call any Set-, New- or Remove- cmdlet and changes nothing in your tenant. It does not read email content. The JSON file stays on your computer until YOU upload it on the TenantPulse "Email deep-check" page. .REQUIREMENTS - PowerShell 5.1 or 7+ - ExchangeOnlineManagement module: Install-Module ExchangeOnlineManagement -Scope CurrentUser - An account with a read-only Exchange role (e.g. Global Reader, Security Reader or View-Only Organization Management) .EXAMPLE .\TenantPulse-ExchangeCheck.ps1 -OutFile .\tenantpulse-exchange.json #> param( [string]$OutFile = ".\tenantpulse-exchange.json" ) $ErrorActionPreference = 'Stop' if (-not (Get-Module -ListAvailable -Name ExchangeOnlineManagement)) { Write-Host "The ExchangeOnlineManagement module is missing. Install it with:" -ForegroundColor Yellow Write-Host " Install-Module ExchangeOnlineManagement -Scope CurrentUser" exit 1 } # Sign in interactively (read-only session) Connect-ExchangeOnline -ShowBanner:$false function ConvertTo-StringList($value) { if ($null -eq $value) { return @() } return @($value | ForEach-Object { [string]$_ }) } try { Write-Host "1/4 Reading mailboxes with forwarding..." $allMailboxes = @(Get-Mailbox -ResultSize Unlimited) $mailboxes = @( $allMailboxes | Where-Object { $_.ForwardingSmtpAddress -or $_.ForwardingAddress } | ForEach-Object { [ordered]@{ UserPrincipalName = [string]$_.UserPrincipalName ForwardingSmtpAddress = [string]$_.ForwardingSmtpAddress ForwardingAddress = [string]$_.ForwardingAddress DeliverToMailboxAndForward = [bool]$_.DeliverToMailboxAndForward } } ) Write-Host "2/4 Reading inbox rules for $($allMailboxes.Count) mailboxes (this can take a while)..." $rules = [System.Collections.Generic.List[object]]::new() $i = 0 foreach ($mbx in $allMailboxes) { $i++ if ($i % 25 -eq 0) { Write-Host " $i / $($allMailboxes.Count)" } try { $found = @(Get-InboxRule -Mailbox $mbx.UserPrincipalName -IncludeHidden -ErrorAction Stop) } catch { continue # mailbox without access or not a real mailbox: skip } foreach ($r in $found) { if ($r.ForwardTo -or $r.RedirectTo -or $r.ForwardAsAttachmentTo -or $r.DeleteMessage -or $r.MoveToFolder) { $rules.Add([ordered]@{ Mailbox = [string]$mbx.UserPrincipalName Name = [string]$r.Name Enabled = [bool]$r.Enabled ForwardTo = ConvertTo-StringList $r.ForwardTo RedirectTo = ConvertTo-StringList $r.RedirectTo ForwardAsAttachmentTo = ConvertTo-StringList $r.ForwardAsAttachmentTo DeleteMessage = [bool]$r.DeleteMessage MoveToFolder = [string]$r.MoveToFolder }) } } } Write-Host "3/4 Reading outbound spam policies..." $spam = @( Get-HostedOutboundSpamFilterPolicy | ForEach-Object { [ordered]@{ Name = [string]$_.Name; IsDefault = [bool]$_.IsDefault; AutoForwardingMode = [string]$_.AutoForwardingMode } } ) Write-Host "4/4 Reading remote domains..." $remote = @( Get-RemoteDomain | ForEach-Object { [ordered]@{ Name = [string]$_.Name; DomainName = [string]$_.DomainName; AutoForwardEnabled = [bool]$_.AutoForwardEnabled } } ) $result = [ordered]@{ schema = 'tenantpulse-exchange-v1' generatedUtc = (Get-Date).ToUniversalTime().ToString('o') mailboxCount = $allMailboxes.Count mailboxes = $mailboxes inboxRules = @($rules) outboundSpamPolicies = $spam remoteDomains = $remote } $json = ConvertTo-Json -InputObject $result -Depth 6 $full = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutFile) [System.IO.File]::WriteAllText($full, $json, [System.Text.UTF8Encoding]::new($false)) Write-Host "Done. Upload this file on the TenantPulse Email deep-check page: $OutFile" -ForegroundColor Green } finally { Disconnect-ExchangeOnline -Confirm:$false | Out-Null }