Email for our users is redirected from the email environment at our corporate headquarters, and if we remove a mailbox in our Exchange 2010 environment without telling HQ to remove the redirect, an email loop will occur.
This doesn’t occur very often, but when it does, it’s good to take care of it right away. Therefore, I wrote a short PowerShell script that checks for messages with a “Local Loop Detected” error. When a message has this error, the script pulls out the recipient’s email address, sends an email to our IT group with the email address, and then removes the message from the queue.
The following PowerShell script was written for Exchange 2010, and runs every 10 minutes on our Edge Transport server.
## Setup the Email $FromAddr = "someGroup@company.com" $ToAddr = "someGroup@company.com" $Subject = "Email Loop Detected" $SendingServer = "localhost" ## Check messages for local loop error $qMessages = get-message -filter {LastError -eq "A local loop was detected."} if ( $qMessages -ne $null ) { ## If the message error exists, do the following for each message foreach ( $qM in $qMessages) { ## Get the recipient email address $qTemp = get-message -id $qM.Identity -includerecipientinfo | ft @{label="Recipient Email";expression={$_.Recipients}} | out-string ## Add instructions to the email body $MessageHeader = "Put a message here. `n" ## Add the above note and the recipient email to the email body $MessageBody = $MessageHeader + $qTemp ##Send the message send-mailmessage -from $FromAddr -to $ToAddr -subject $Subject -body $MessageBody -smtpServer $SendingServer ## Remove the email from the queue remove-message -id $qM.Identity -Confirm:$False } }