Skip to main content

Oversize mail reporting with Monad

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)

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Gr...

EWS-FAI Module for browsing and updating Exchange Folder Associated Items from PowerShell

Folder Associated Items are hidden Items in Exchange Mailbox folders that are commonly used to hold configuration settings for various Mailbox Clients and services that use Mailboxes. Some common examples of FAI's are Categories,OWA Signatures and WorkHours there is some more detailed documentation in the https://msdn.microsoft.com/en-us/library/cc463899(v=exchg.80).aspx protocol document. In EWS these configuration items can be accessed via the UserConfiguration operation https://msdn.microsoft.com/en-us/library/office/dd899439(v=exchg.150).aspx which will give you access to either the RoamingDictionary, XMLStream or BinaryStream data properties that holds the configuration depending on what type of FAI data is being stored. I've written a number of scripts over the years that target particular FAI's (eg this one that reads the workhours  http://gsexdev.blogspot.com.au/2015/11/finding-timezone-being-used-in-mailbox.html is a good example ) but I didn't have a gene...

Sending a MimeMessage via the Microsoft Graph using the Graph SDK, MimeKit and MSAL

One of the new features added to the Microsoft Graph recently was the ability to create and send Mime Messages (you have been able to get Message as Mime for a while). This is useful in a number of different scenarios especially when trying to create a Message with inline Images which has historically been hard to do with both the Graph and EWS (if you don't use MIME). It also opens up using SMIME for encryption and a more easy migration path for sending using SMTP in some apps. MimeKit is a great open source library for parsing and creating MIME messages so it offers a really easy solution for tackling this issue. The current documentation on Send message via MIME lacks any real sample so I've put together a quick console app that use MSAL, MIME kit and the Graph SDK to send a Message via MIME. As the current Graph SDK also doesn't support sending via MIME either there is a workaround for this in the future my guess is this will be supported.
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.