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.
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.
This script contains two advanced functions, Suspend-OSCEXMessage and Resume-OSCEXSuspendedMessage. You can use this script in following ways:
Method 1:
Method 2:
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
}
}
}
}
}
}
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 } } } } } }
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:
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