I wanted some transparency into the transport queue on Exchange 2010 mail servers to check or identify potential email failures or delays. Most of the time these are emails that were sent to “no-reply” addresses or typos in the recipient’s email address.
I wrote a quick PowerShell script that checks each transport server for any queue that has one or more messages, finds each email in that queue, and then searches message tracking logs regarding that message.
It uses my “go-to” HTML Email theme/template to compose an email with the nextHopDomain and each message for that domain. This includes some common info to help identify the message, sender, recipient, error, and etc.
I’m sure the script can be tweaked or modified, but I wanted to use functions so I can use them for other scripts. Modify the email settings/values for your needs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <################################################################## ## Check Trasnsport Servers NDR emails in queues and report to ## ## administrators via eamil ## ## ## ## Modify values under VARS section for email settings ## ## ## ###################################################################> #Add Exchange Tools Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue; ## START VARS $emails = @( "email1@domain.com" ,"email2@domain.com" ) $mailSubject = "Daily NDR Report for DOMAIN" $mailServer = "mail.domain.com" $fromAddress = "alerts@domain.com" ## FUNCTIONS function msg_get_queue() { $mQ = @() $mQ = Get-ExchangeServer | ? {$_.isHubTransportServer -eq $true} | get-queue | ? {$_.MessageCount -gt 0} return $mQ } function msg_get_msgFromQ($i) { $mFQ = @() foreach ($x in $i) { $mFQ += ($x | Get-Message) } return $mFQ } function msg_get_details($i) { $mQD = @() foreach ($svr in (Get-ExchangeServer | ? {$_.isHubTransportServer -eq $true})) { $mQD += Get-MessageTrackingLog -MessageId $i -Server $svr.Name | ? {$_.EventID -eq "RECEIVE"} } return $mQD } ## FUNCTION TO GET Q AND BUILD EMAIL function msg_get_NDR_details() { $html = " <meta http-equiv="" content="" charset="us-asciiamp;"amp;"" /> <meta name="Generator" content="" /> |
foreach ($subQ in (msg_get_queue)) {
foreach ($subMsg in $subQ) {
$html += ”
$($subMsg.NextHopDomain) $($subMsg.Status) due to $($subMsg.DeliveryType), will try again on $($subMsg.NextRetryTime) Last Error: $($subMsg.LastError) | ||||||||||||||
” foreach ( $subDetails in (msg_get_msgFromQ($subMsg)) ) { $rows = msg_get_details($subDetails.InternetMessageId) foreach ($row in $rows) { $html += ” ” } } $html += ”
| ||||||||||||||
Total Messages in this Queue: $($subMsg.MessageCount) |
”
}
}
$html += ”
1 2 3 4 5 6 7 | " return $html } #SEND EMAIL Send-MailMessage -SmtpServer $mailServer -To $emails -From $fromAddress -subject $mailSubject -bodyAsHtml (msg_get_NDR_details) |
Excellent article. I’m experiencing msny of these issues as well..
Get an small brief idea for PowerShell script to Identify Delays or Failures in Exchange Transport Queue and send Email. Use the post.