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

Export calendar Items to a CSV file using Microsoft Graph and Powershell

For the last couple of years the most constantly popular post by number of views on this blog has been  Export calendar Items to a CSV file using EWS and Powershell closely followed by the contact exports scripts. It goes to show this is just a perennial issue that exists around Mail servers, I think the first VBS script I wrote to do this type of thing was late 90's against Exchange 5.5 using cdo 1.2. Now it's 2020 and if your running Office365 you should really be using the Microsoft Graph API to do this. So what I've done is create a PowerShell Module (and I made it a one file script for those that are more comfortable with that format) that's a port of the EWS script above that is so popular. This script uses the ADAL library for Modern Authentication (which if you grab the library from the PowerShell gallery will come down with the module). Most EWS properties map one to one with the Graph and the Graph actually provides better information on recurrences then...

Downloading a shared file from Onedrive for business using Powershell

I thought I'd quickly share this script I came up with to download a file that was shared using One Drive for Business (which is SharePoint under the covers) with Powershell. The following script takes a OneDrive for business URL which would look like https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filename.txt This script is pretty simple it uses the SharePoint CSOM (Client side object Model) which it loads in the first line. It uses the URI object to separate the host and relative URL which the CSOM requires and also the SharePointOnlineCredentials object to handle the Office365 SharePoint online authentication. The following script is a function that take the OneDrive URL, Credentials for Office365 and path you want to download the file to and downloads the file. eg to run the script you would use something like ./spdownload.ps1 ' https://mydom-my.sharepoint.com/personal/gscales_domain_com/Documents/Email%20attachments/filen...

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.