These days spikes in email traffic could mean anything from its Valentine's Day and your email server is being flooded with Ecards, newsletters or the latest funny video clip that people just had to send to every person in there contacts list. So when you receive that call that email is slow message tracking is a good place to start. The one bad thing is that while the Message Tacking Center GUI is functional it lacks the basic ability to export the logs. Which is exactly what I wanted to do this week so I could ship it off to someone to say here’s your problem “with the tag so do you still think having no message restrictions is a good idea“. The good thing about message tracking logs is that they are accessible using WMI so running quick live reports is pretty easier with a script. Last month Microsoft planked out Beta 3 of monad so I thought it was time to update and give it another test drive with this task.
Msh’s ability to present things in a tabular format is very cool the ability to which you can format the result with format-table and a hash table means it leaves VBS eating its dust.
The Script
The script itself is very easy it takes 3 command line parameters the first is the server you want to run it against the second is the number of hours to look back in the logs and the third is the size in MB for mails to report on. The script takes these three parameters and queries the server via WMI for any mail in the message tracking logs over a certain size within the time frame specified and produces a report. The first version of the script just displays this to the commandline. The second version uses the new System.Net.Mail.MailMessage namespace to send a message over SMTP where the body is a HTML table of the results of the query. The script will try to send the email by port 25 of the server you are reporting on. To use this script you first need to configure a sending and recieving email address within the script.
To run the script to report on email over 2MB for the last 8 hours use something like c:\osizedisp.msh servername 8 2
I’ve put a download copy of the scripts here
The script itself looks like
param([String] $servername = $(throw "Please specify the Servername"),
[int32] $timerange = $(throw "Please specify a Time Range in Hours"),[int32] $sizeg
= $(throw "Please specify the lower size limit"))
$dtQueryDT = [DateTime]::UtcNow.AddHours(-$timerange)
$sizeg = $sizeg * 1024 * 1024
$WmidtQueryDT = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime($dtQueryDT)
$WmiNamespace = "ROOT\MicrosoftExchangev2"
$filter = "entrytype = '1020' and OriginationTime >= '" + $WmidtQueryDT + "' and
size > " + $sizeg + " or entrytype = '1028' and OriginationTime >= '" + $WmidtQueryDT
+ "' and size > " + $sizeg
$Qresults = get-wmiobject -class Exchange_MessageTrackingEntry -Namespace $WmiNamespace
-ComputerName $servername -filter $filter
$format = @{expression={[System.Management.ManagementDateTimeConverter]::ToDateTime($_.OriginationTime)};width=22;Label="Time"},@{Expression
= {$_.Senderaddress};width=18;Label = "Sender"},@{Expression = {$_.recipientaddress};width=18;Label
= "Recipient"},@{Expression ={$_.Subject};width=30;Label
="Subject"},@{Expression = {($_.Size)/1024/1024}; Format = "{0:N2} MB";Label =
"Size MB"}
$Qresults | format-table $format
The Second script looks like
param([String] $servername = $(throw "Please specify the Servername"),
[int32] $timerange = $(throw "Please specify a Time Range in Hours"),[int32] $sizeg
= $(throw "Please specify the lower size limit"))
$dtQueryDT = [DateTime]::UtcNow.AddHours(-$timerange)
$sizegdiv = $sizeg * 1024 * 1024
$WmidtQueryDT = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime($dtQueryDT)
$WmiNamespace = "ROOT\MicrosoftExchangev2"
$filter = "entrytype = '1020' and OriginationTime >= '" + $WmidtQueryDT + "' and
size > " + $sizegdiv + " or entrytype = '1028' and OriginationTime >= '" + $WmidtQueryDT
+ "' and size > " + $sizegdiv
$Qresults = get-wmiobject -class Exchange_MessageTrackingEntry -Namespace $WmiNamespace
-ComputerName $servername -filter $filter
$BodyTable = "<table border=`"1`" cellpadding=`"0`" cellspacing=`"0`"
width=`"100%`">`r<tr>`r"
$td = "<td width=`"20%`" align=`"center`">"
$BodyTable = $BodyTable + "<tr>" + $td + "Date/Time</td>" + $td + "From</td>" +
$td + "Sent-to</td>" + $td + "Subject</td>" + $td + "Size (MB)</td></tr>"
foreach ($Mentry in $Qresults){
$BodyTable = $BodyTable + "<td align=`"center`">" + [System.Management.ManagementDateTimeConverter]::ToDateTime($Mentry.OriginationTime)
+ "</td>`r"
$BodyTable = $BodyTable + "<td align=`"center`">" + $Mentry.Senderaddress +
"</td>`r"
$BodyTable = $BodyTable + "<td align=`"center`">" + $Mentry.recipientaddress +
"</td>`r"
$BodyTable = $BodyTable + "<td align=`"center`">" + $Mentry.subject + "</td>`r"
$BodyTable = $BodyTable + "<td align=`"center`">" + ($Mentry.size/1024/1024).tostring("0.00")
+ "</td>`r"
$BodyTable = $BodyTable + "</tr>`r"
}
$SmtpClient = new-object system.net.mail.smtpClient
$SmtpClient.host = $servername
$MailMessage = new-object System.Net.Mail.MailMessage
$MailMessage.To.Add("youruser@domain.com")
$MailMessage.From = "source@domain.com"
$MailMessage.Subject = "Messages larger then " + $sizeg + " MB for the past " +
$timerange + " Hours on Server " + $servername
$MailMessage.IsBodyHtml = $TRUE
$MailMessage.body = $BodyTable
$SMTPClient.Send($MailMessage)
Msh’s ability to present things in a tabular format is very cool the ability to which you can format the result with format-table and a hash table means it leaves VBS eating its dust.
The Script
The script itself is very easy it takes 3 command line parameters the first is the server you want to run it against the second is the number of hours to look back in the logs and the third is the size in MB for mails to report on. The script takes these three parameters and queries the server via WMI for any mail in the message tracking logs over a certain size within the time frame specified and produces a report. The first version of the script just displays this to the commandline. The second version uses the new System.Net.Mail.MailMessage namespace to send a message over SMTP where the body is a HTML table of the results of the query. The script will try to send the email by port 25 of the server you are reporting on. To use this script you first need to configure a sending and recieving email address within the script.
To run the script to report on email over 2MB for the last 8 hours use something like c:\osizedisp.msh servername 8 2
I’ve put a download copy of the scripts here
The script itself looks like
param([String] $servername = $(throw "Please specify the Servername"),
[int32] $timerange = $(throw "Please specify a Time Range in Hours"),[int32] $sizeg
= $(throw "Please specify the lower size limit"))
$dtQueryDT = [DateTime]::UtcNow.AddHours(-$timerange)
$sizeg = $sizeg * 1024 * 1024
$WmidtQueryDT = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime($dtQueryDT)
$WmiNamespace = "ROOT\MicrosoftExchangev2"
$filter = "entrytype = '1020' and OriginationTime >= '" + $WmidtQueryDT + "' and
size > " + $sizeg + " or entrytype = '1028' and OriginationTime >= '" + $WmidtQueryDT
+ "' and size > " + $sizeg
$Qresults = get-wmiobject -class Exchange_MessageTrackingEntry -Namespace $WmiNamespace
-ComputerName $servername -filter $filter
$format = @{expression={[System.Management.ManagementDateTimeConverter]::ToDateTime($_.OriginationTime)};width=22;Label="Time"},@{Expression
= {$_.Senderaddress};width=18;Label = "Sender"},@{Expression = {$_.recipientaddress};width=18;Label
= "Recipient"},@{Expression ={$_.Subject};width=30;Label
="Subject"},@{Expression = {($_.Size)/1024/1024}; Format = "{0:N2} MB";Label =
"Size MB"}
$Qresults | format-table $format
The Second script looks like
param([String] $servername = $(throw "Please specify the Servername"),
[int32] $timerange = $(throw "Please specify a Time Range in Hours"),[int32] $sizeg
= $(throw "Please specify the lower size limit"))
$dtQueryDT = [DateTime]::UtcNow.AddHours(-$timerange)
$sizegdiv = $sizeg * 1024 * 1024
$WmidtQueryDT = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime($dtQueryDT)
$WmiNamespace = "ROOT\MicrosoftExchangev2"
$filter = "entrytype = '1020' and OriginationTime >= '" + $WmidtQueryDT + "' and
size > " + $sizegdiv + " or entrytype = '1028' and OriginationTime >= '" + $WmidtQueryDT
+ "' and size > " + $sizegdiv
$Qresults = get-wmiobject -class Exchange_MessageTrackingEntry -Namespace $WmiNamespace
-ComputerName $servername -filter $filter
$BodyTable = "<table border=`"1`" cellpadding=`"0`" cellspacing=`"0`"
width=`"100%`">`r<tr>`r"
$td = "<td width=`"20%`" align=`"center`">"
$BodyTable = $BodyTable + "<tr>" + $td + "Date/Time</td>" + $td + "From</td>" +
$td + "Sent-to</td>" + $td + "Subject</td>" + $td + "Size (MB)</td></tr>"
foreach ($Mentry in $Qresults){
$BodyTable = $BodyTable + "<td align=`"center`">" + [System.Management.ManagementDateTimeConverter]::ToDateTime($Mentry.OriginationTime)
+ "</td>`r"
$BodyTable = $BodyTable + "<td align=`"center`">" + $Mentry.Senderaddress +
"</td>`r"
$BodyTable = $BodyTable + "<td align=`"center`">" + $Mentry.recipientaddress +
"</td>`r"
$BodyTable = $BodyTable + "<td align=`"center`">" + $Mentry.subject + "</td>`r"
$BodyTable = $BodyTable + "<td align=`"center`">" + ($Mentry.size/1024/1024).tostring("0.00")
+ "</td>`r"
$BodyTable = $BodyTable + "</tr>`r"
}
$SmtpClient = new-object system.net.mail.smtpClient
$SmtpClient.host = $servername
$MailMessage = new-object System.Net.Mail.MailMessage
$MailMessage.To.Add("youruser@domain.com")
$MailMessage.From = "source@domain.com"
$MailMessage.Subject = "Messages larger then " + $sizeg + " MB for the past " +
$timerange + " Hours on Server " + $servername
$MailMessage.IsBodyHtml = $TRUE
$MailMessage.body = $BodyTable
$SMTPClient.Send($MailMessage)