Scenario: We noticed that messages were taking longer to deliver when ATP Safe Attachments was being called for message processing. Users in the Dynamic action policy noticed that the attachment would be reinserted hours after the message delivery. Other users in a Replace action policy noticed that the entire message was delayed delivery for hours.
Script: We found a script online that we modified to fit our needs. Here is our modified version of the script:
ATP Safe Attachment Scanning Delay Scriptlets
#Declare Variables
$Outfile = “C:\temp\ATPSafeAttachmentScan-$(Get-Date -Format “MMddyyyy_hhmmss”).csv”
$MessageList = $null
$CurrMessages = $null
$Page = 1
$PageSize = 5000
$start = (Get-Date).AddHours(-4)
$end = Get-Date
$MessgaeListCount = 0
#Loop for Message Trace
do
{
Write-Host “Collecting Message Trace – Current Count: $MessageListCount – Page $Page…”
$CurrMessages = Get-MessageTrace -StartDate $start -EndDate $end -PageSize $pagesize -Page $Page -Status Delivered | Where {$_.Size -gt 1MB}
$Page++
$MessageList += $CurrMessages
$MessageListCount = $messageList.count
} until ($CurrMessages -eq $null)
#Loop Message Trace Results for ATP Events
$Row = 0
$TotalRow = $MessageList.count
$MessageList | Select-Object -Skip $Row | % {
$ID = ($_.MessageTraceId).Guid
$Sender = $_.SenderAddress
$Recipient = $_.RecipientAddress
$Size = $_.Size
$MessageDetails = $_ | Get-MessageTraceDetail | where { $_.Event -eq “Advanced Threat Protection” -or $_.Event -eq “Deliver”} | sort Date
$First = ($MessageDetails | select -First 1).Date
$Last = ($MessageDetails | select -Last 1).Date
# Only if ATP was used
If ($First -ne $null -and $Last -ne $null) {
If ($First -eq $Last) {$Delay = 0}
Else { $Delay = [math]::Round((New-TimeSpan –Start $First –End $Last).TotalSeconds,0) }
$Item = New-Object System.Object;
$Item | Add-Member -Type NoteProperty -Name “ID” -Value $ID;
$Item | Add-Member -Type NoteProperty -Name “Sender” -Value $Sender;
$Item | Add-Member -Type NoteProperty -Name “Recipient” -Value $Recipient;
$Item | Add-Member -Type NoteProperty -Name “Size” -Value $Size; $Item | Add-Member -Type NoteProperty -Name “Start” -Value $First;
$Item | Add-Member -Type NoteProperty -Name “End” -Value $Last;
$Item | Add-Member -Type NoteProperty -Name “Delay” -Value $Delay;
#$results += $Item
$Item | Export-Csv $Outfile -NoTypeInformation -Append }
$Row++
Write-Host “$(Get-Date -Format “HH:mm”): Processed row $Row out of $TotalRow” }
}
#REF: Original Script that we modified: https://jocha.se/blog/tech/exchange-atp-attachment-delay