Manage Large Messages in Microsoft Exchange 2010

Introduction

This script could be used to manage large messages in Microsoft Exchange 2010. You can use this script for suspending or resuming large messages. Meanwhile, this script will send notifications to these users who are sending large messages.

Scenarios

In a real world, IT administrators may want to find out the users who are sending lager messages. They hope these messages will be delivered at afterhours. Large quantity of newsletter messages is a typical scenario for using this script.

Script

This script contains two advanced functions, Suspend-OSCEXMessage and Resume-OSCEXSuspendedMessage. You can use this script in following ways:

Method 1:

  1. Download the script and copy it to a Microsoft Exchange 2010 Hub or Edge transport servers.
  2. Open the script file with Notepad or any other script editors.
  3. Scroll down to the end of the script file, and then add the example command which you want to run.
  4. Save the file then run the script in Exchange Management Shell.

Method 2:

  1. Rename scriptname.ps1 to scriptname.psm1 (PowerShell Module file)
  2. Run Import-Module cmdlet to import this module file in Exchange Management Shell.
     Import-Module filepath\scriptname.psm1

 

PowerShell
Edit|Remove
Function Suspend-OSCEXMessage 
{ 
    [CmdletBinding(DefaultParameterSetName="__AllParameterSets")] 
    Param 
    ( 
        #Define parameters 
        [Parameter(Mandatory=$true,Position=1)] 
        [string[]]$TransportServer, 
        [Parameter(Mandatory=$true,Position=2)] 
        [string]$MessageFilter, 
        [Parameter(Mandatory=$false,Position=3)] 
        [string[]]$MessageProperty="*",         
        [Parameter(Mandatory=$true,Position=4,ParameterSetName="DoSuspend")] 
        [switch]$SuspendMailDelivery, 
        [Parameter(Mandatory=$true,Position=5,ParameterSetName="DoSuspend")] 
        [string]$NotificationMailFrom,         
        [Parameter(Mandatory=$true,Position=6,ParameterSetName="DoSuspend")] 
        [string[]]$NotificationMailCC, 
        [Parameter(Mandatory=$true,Position=7,ParameterSetName="DoSuspend")] 
        [string]$NotificationMailSmtpServer                 
    ) 
    Process 
    { 
        $mailMsgs = @() 
        #Retrieve messages from multiple transport servers 
        foreach ($transServer in $TransportServer) { 
            Try 
            { 
                $mailMsg = Get-Message -Server $transServer -Filter $MessageFilter -Verbose:$false 
            } 
            Catch 
            { 
                $pscmdlet.WriteError($Error[0]) 
            } 
            if ($mailMsg -eq $null) { 
                $verboseMsg = $Messages.CannotFindMessages 
                $verboseMsg = $verboseMsg -replace "Placeholder01",$transServer 
                $pscmdlet.WriteVerbose($verboseMsg) 
            } else { 
                $mailMsgs +$mailMsg 
            } 
        } 
        #Processing the mails 
        if ($mailMsgs -ne $null) { 
            Switch ($pscmdlet.ParameterSetName) { 
                "__AllParameterSets" { 
                    #Display the messages which meet the filter. 
                    $mailMsgs | Select-Object -Property $MessageProperty 
                } 
                "DoSuspend" { 
                    if ($SuspendMailDelivery) { 
                        $SuspendMessageDeliverySubject = $Messages.SuspendMessageDeliverySubject 
                        $SuspendMessageDeliveryBody = $Messages.SuspendMessageDeliveryBody 
                        #Try to suspend messages and send notifications 
                        foreach ($mailMsg in $mailMsgs) { 
                            Try 
                            { 
                                if ($mailMsg.Queue -notmatch "Submission|Poison") { 
                                    Suspend-Message -Identity $($mailMsg.Identity) -Confirm:$false -Verbose:$false -ErrorAction:Stop 
                                } else { 
                                    $verboseMsg = $Messages.CannotSuspendAMessageinSubmissionPoisonQueue 
                                    $verboseMsg = $verboseMsg -replace "Placeholder01",$($mailMsg.Identity) 
                                    $pscmdlet.WriteVerbose($verboseMsg) 
                                } 
                            } 
                            Catch 
                            { 
                                $pscmdlet.WriteWarning($Error[0]) 
                            } 
                            $suspendStatus = (Get-Message -Identity $($mailMsg.Identity)).Status 
                            if ($suspendStatus -eq "Suspended") { 
                                $isSuspended = $true 
                            } elseif ($suspendStatus -eq "PendingSuspend") { 
                                $isSuspended = $false 
                                $warningMsg = $Messages.PendingSuspend 
                                $warningMsg = $warningMsg -replace "Placeholder01",$($mailMsg.Identity) 
                                $pscmdlet.WriteWarning($warningMsg) 
                            }                             
                            if ($isSuspended) { 
                                $verboseMsg = $Messages.SendingMessage 
                                $verboseMsg = $verboseMsg -replace "Placeholder01","$($mailMsg.FromAddress)" 
                                $pscmdlet.WriteVerbose($verboseMsg) 
                                $SuspendMessageDeliveryBody = $SuspendMessageDeliveryBody -replace "Placeholder01","$($mailMsg.Subject)" 
                                $SuspendMessageDeliveryBody = $SuspendMessageDeliveryBody -replace "Placeholder02","$($mailMsg.Identity)" 
                                Send-MailMessage -SmtpServer $NotificationMailSmtpServer ` 
                                -From $NotificationMailFrom -To $($mailMsg.FromAddress) -Cc $NotificationMailCC ` 
                                -Subject $SuspendMessageDeliverySubject -Body $SuspendMessageDeliveryBody 
                            } 
                        } 
                    } else { 
                        #Send notification mail to mail system administrators. 
                        $verboseMsg = $Messages.SendingMessage 
                        $verboseMsg = $verboseMsg -replace "Placeholder01",$NotificationMailCC 
                        $pscmdlet.WriteVerbose($verboseMsg) 
                        $NotificationMailSubjectForMultipleMails = $Messages.NotificationMailSubjectForMultipleMails 
                        $NotificationMailSubjectForMultipleMails = $NotificationMailSubjectForMultipleMails -replace "Placeholder01",$($mailMsgs.Count) 
                        $notificationMailBody = $mailMsgs | Select-Object -Property $MessageProperty | ConvertTo-Html -Fragment 
                        Send-MailMessage -SmtpServer $NotificationMailSmtpServer ` 
                        -From $NotificationMailFrom -To $NotificationMailCC ` 
                        -Subject $NotificationMailSubjectForMultipleMails -Body "$notificationMailBody" -BodyAsHtml 
                    } 
                } 
            } 
        } 
    } 
}
 

 

Examples

Example 01: Displays help about Suspend-OSCEXMessage
Command: Get-Help Suspend-OSCEXMessage -Full
Screenshot:

 

Example 02: Use Suspend-OSCEXMessage for monitoring large size messages and displays the results in the console.
Command:
 $messageSize = "10KB"
 1..20 | %{
                Suspend-OSCEXMessage -TransportServer "transportserver01"," transportserver02" -MessageFilter '(Size -gt $messageSize) -and (Status -ne "Suspended")' -MessageProperty "Identity","FromAddress","Size","Subject" -Verbose
                Start-Sleep -Seconds 1
 }
Screenshot:

 

Note: Since the script is running in a lab environment, it uses “10KB” instead of “10MB”, you should use a proper value in your environment.

Example 03: Use Suspend-OSCEXMessage for monitoring large size messages and send emails to administrators only. Messages will not be suspended.
Command:
 $messageSize = "10KB"
 1..20 | %{
                Suspend-OSCEXMessage -TransportServer "transportserver01","transportserver02" -MessageProperty "Identity","FromAddress","Size","Subject" `
                -MessageFilter '(Size -gt $messageSize) -and (Status -ne "Suspended")' -SuspendMailDelivery:$false -NotificationMailSmtpServer transportserver01 `
                -NotificationMailFrom "itadmin@corp.contoso.com" -NotificationMailCC "itadmin@corp.contoso.com" -Verbose
                Start-Sleep -Seconds 1
 }
 Screenshot:

 

Note: Since the script is running in a lab environment, it uses “10KB” instead of “10MB”, you should use a proper value in your environment.

Example 04:
 Use Suspend-OSCEXMessage for monitoring large size messages and send emails to administrators and end users.
 Command:
 $messageSize = "10KB"
 1..20 | %{
                Suspend-OSCEXMessage -TransportServer "transportserver01","transportserver02" -MessageProperty "Identity","FromAddress","Size","Subject" `
                -MessageFilter '(Size -gt $messageSize) -and (Status -ne "Suspended")' -SuspendMailDelivery -NotificationMailSmtpServer transportserver01 `
                -NotificationMailFrom "itadmin@corp.contoso.com" -NotificationMailCC "itadmin@corp.contoso.com" -Verbose
                Start-Sleep -Seconds 1
 }
Screenshot:

 

Note: Since the script is running in a lab environment, it uses “10KB” instead of “10MB”, you should use a proper value in your environment.

Example 05:
 Use Suspend-OSCEXMessage for monitoring messages with specific subject and send emails to administrators and end users.
 Command:
 $messageSubject = "Newsletter*"
 1..20 | %{
                Suspend-OSCEXMessage -TransportServer "transportserver01","transportserver02" -MessageProperty "Identity","FromAddress","Size","Subject" `
                -MessageFilter '(Subject -like $messageSubject) -and (Status -ne "Suspended")' -SuspendMailDelivery -NotificationMailSmtpServer transportserver01 `
                -NotificationMailFrom "itadmin@corp.contoso.com" -NotificationMailCC "itadmin@corp.contoso.com" -Verbose
                Start-Sleep -Seconds 1
 }
Screenshot:

 

Example 06:
 Confirm the messages that will be resumed.
 Command:
 Resume-OSCEXSuspendedMessage -TransportServer "transportserver01","transportserver02" -MessageFilter 'Status -eq "Suspended"' -Verbose -Whatif                               
Screenshot:

 

Example 07:
 Resume the suspended messages by using specific filter.
 Command:
 Resume-OSCEXSuspendedMessage -TransportServer "transportserver01","transportserver02" -MessageFilter 'Status -eq "Suspended"' -Verbose                               
Screenshot:

 

 

Additional Resources

Technical Resource:

Windows PowerShell Advanced Function
http://technet.microsoft.com/en-us/library/dd315326.aspx

Get-Message
http://technet.microsoft.com/en-us/library/bb124738.aspx

Suspend-Message
http://technet.microsoft.com/en-us/library/aa997457.aspx

Resume-Message
http://technet.microsoft.com/en-us/library/aa997457.aspx

Understanding Transport Queues
http://technet.microsoft.com/en-us/library/bb125022.aspx 

Forum Threads:

How to capture large emails in queue and send it via email? (Powershell script)
http://social.technet.microsoft.com/Forums/en-US/exchangesvrmonitoring/thread/3257bb94-7923-4625-9451-0f6355aa6f08