Track Exchange Mail Flow Rule Activity Beyond the 10-Day Report Window
- Tony Redmond
- 816 views
An Azure Automation runbook can save weekly mail flow rule counts to SharePoint, building a longer history of suppressed email by domain.

Exchange Online’s mail flow rule report gives administrators only a short window into rule activity: Get-MailDetailTransportRuleReport returns data for the last ten days. For teams that delete known spam automatically, that makes it difficult to track suppression volumes and identify which domains account for the messages over longer periods. A scheduled Azure Automation runbook can preserve those weekly results in a SharePoint Online CSV file for later analysis.
Why keep a longer record?
A mail flow rule can delete inbound messages from known spammer domains before they reach users. The rule described here currently targets 95 domains. Its operator maintains that list as spammers change their domains and tactics. Microsoft Defender for Office 365 can quarantine detected spam, while immediate deletion through the rule avoids sending users notifications about quarantined messages.
Tracking the rule’s activity adds a measure of how much email it handles and which domains are responsible. That information can help administrators review whether the rule is still addressing the traffic they expect. The Exchange admin center also warns administrators when they add or change a mail flow rule that affects mail delivery.

The reporting cmdlet has a narrower lookback than Get-MessageTraceV2. Message trace data can be collected in multiple ten-day intervals to reach back as far as 90 days; the mail flow rule report doesn’t offer the same way to retrieve older periods. Administrators who need a longer record therefore have to collect the report data before it ages out.
Build history with a weekly runbook
The approach is to run an Azure Automation job weekly and capture the previous seven days of mail flow suppression data. The job stores the results in a CSV file in a SharePoint Online document library, updating that file each week. Each run adds another period of counts by spammer domain, creating a historical dataset that can be loaded into Power BI or another analysis tool.
The weekly schedule fits within the cmdlet’s ten-day reporting window, leaving room for the job to collect the full seven-day period. The accumulated CSV can then support comparisons across weeks and show how the volume associated with each domain changes over time. It also gives administrators a data source they can examine outside the reporting cmdlet’s limited lookback.
The script is available in the Microsoft 365 GitHub repository. Its original file operations use Microsoft Graph PowerShell SDK cmdlets to locate, download and update the CSV in SharePoint. Those operations work in interactive delegated or app-only sessions, but the Microsoft.Graph.Files module has a known issue in Azure Automation. The problem remains in Graph SDK version V2.40, described as the latest “good” version.
Use managed identities and direct Graph requests for files
To run the collection job in Azure Automation, the script connects to Exchange Online and Microsoft Graph with a managed identity. It also replaces the affected Graph SDK file commands with direct Graph API requests, sent through Invoke-MgGraphRequest.
For example, the SDK command below writes the CSV file to the target drive location:
$TargetLocation = "$FolderPath/$SpammyEmailHistoricalFile"
$SpamFileItem = Set-MgDriveItemContent -DriveId $Drive.Id -DriveItemId $TargetLocation -InFile $ExportedSpamDataFile -ErrorAction StopThe runbook instead constructs a Graph API content URL and uploads the file with a PUT request:
$Uri = "https://graph.microsoft.com/v1.0/drives/$($Drive.Id)/root:/$($TargetLocation):/content"
$SpamFileItem = Invoke-MgGraphRequest -Uri $Uri -Method PUT -InputFilePath $ExportedSpamDataFile -ContentType "application/octet-stream" -ErrorAction StopReading the existing CSV before adding new results also requires a file operation. The SDK version uses Get-MgDriveItemContent:
Get-MgDriveItemContent -DriveId $Drive.Id -DriveItemId $SpamFileItem.id -OutFile $DownloadPath -ErrorAction StopIn the runbook, a GET request retrieves the file content to a local path:
$Uri = "https://graph.microsoft.com/v1.0/drives/$($Drive.Id)/items/$($SpamFileItem.Id)/content"
Invoke-MgGraphRequest -Uri $Uri -Method GET -OutputFilePath $DownloadPath -ErrorAction StopThese examples show the change in approach, but they don’t make the conversion a simple command-for-command replacement. The request paths, parameters and file handling need to work together in the target environment, and some trial and error may be needed to get the complete workflow running.
Choose the approach that fits the job
Some developers use Connect-MgGraph to establish a session and then make Graph API requests directly. That keeps token acquisition and session management within the Graph PowerShell connection while avoiding SDK cmdlets for operations that have proved unreliable in a particular environment. This method gives up SDK features such as its retry handler and easier pagination, so those capabilities may need separate attention when a script depends on them.
For administrators who already work in PowerShell, Graph SDK cmdlets can still be the more approachable way to perform many Microsoft 365 tasks. The practical choice depends on which operations work reliably in the execution environment. For this reporting workflow, the key is to preserve the weekly collection: once the ten-day report window has passed, the cmdlet can’t provide those older results. A runbook and SharePoint CSV turn that short-lived report into a dataset administrators can inspect over time.