Skip to main content

Show Users last Message Sent and Received Times,Subject in a Form using Powershell against Exchange 2007 Message Tracking Logs

In the past I’ve written and shown many ways to use Message tracking Log data in Exchange 2000-2003 using the WMI MessageTrackingLog class. With Exchange 2007 the WMI class is now gone replaced with a new Exchange Management Shell Powershell cmdlet. Although losing the richness of WQL is a little bit of shame the new cmdlet is pretty funky especially the little status bar (that’s gold).

Message Tracking Logs can have many uses none more so then showing you current mailbox activity like who is actively sending and receiving mail in the last X number of minutes which may be important to you if you are working late at night on a server and you need to know who is active.

This is my first go at a messagetracking log script for 2007 and I must admit I’m lacking some good load data at the moment for Exchange 2007. What this script does is creates a winform with a few Date and Time controls and populates a Drop down box with the names of the Exchange servers within an Exchange organization using the get-ExchangeServer cmdlet. It then aggregates the message tracking logs results together to show in the time period scaned which users have been sending and receiving messages. How much time has lapsed since they last sent and received something and also what the subject of there last send and received message was.

Determine what is Internal vs. External email

When you are writing a Message Tracking Log script this is usually one of the big challenges for Exchange 2007 I took a slightly different approach by making use of another new EMS cmdlets called Get-AcceptedDomain. This cmdlet allows you to get a list of all the domains that your Exchange org is configured to receive email for which ergo with a bit of parsing becomes your list of internal domains with which you can classify email. For this script the accepted domains are added to a hashtable to allow for comparisons to be done in this script.

Working out the Time since the last message was sent and received.

For this the New-TimeSpan cmdlet from powershell is used. This is a pretty powerful little function it returns a timespan object based on the input of two dates you feed it. In this script I make use of the total minutes to show how much time has elapsed between now and when the users last received and sent an email by comparing the current time and that of the received (or sent) datetime of the last message.

This script doesn’t do any filtering on the type of message tracking log entry retrieved using the get-messagetrackinglog cmdlet and limits the total number of entries it will retrieve using the -ResultSize parameter to 10000 records. Once the script has filtered the entires into a number of hashtables the results are feed back into an ADO.NET datatable and then displayed back to the user in a DataGrid which gives you all the sorting richness etc. If you have included the subject in the Message tracking logs the script will also show the subject of the last sent and received email for each user during the tracking period selected.

This is only really scratching the surface of what you can do with Message tracking Logs in Exchange 2007. I’ve put a downloadable copy of this script here the script itself looks like.

[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$DomainHash = @{ }
$Senders = @{ }
$Recipeints = @{ }
$RecipeintsSubject = @{ }
$SendersSubject = @{ }

function GetLogs(){
$dtQueryDT = New-Object System.DateTime $dpTimeFrom.value.year,$dpTimeFrom.value.month,$dpTimeFrom.value.day,$dpTimeFrom2.value.hour,$dpTimeFrom2.value.minute,$dpTimeFrom2.value.second
$dtQueryDTf = New-Object System.DateTime $dpTimeFrom1.value.year,$dpTimeFrom1.value.month,$dpTimeFrom1.value.day,$dpTimeFrom3.value.hour,$dpTimeFrom3.value.minute,$dpTimeFrom3.value.second
$ltTable.clear()
$Recipeints.clear()
$Senders.clear()
$DomainHash.clear()
$SendersSubject.clear()
$RecipeintsSubject.clear()
get-accepteddomain | ForEach-Object{
if ($_.DomainType -eq "Authoritative"){
$DomainHash.add($_.DomainName.SmtpDomain.ToString().ToLower(),1)
}

}
get-messagetrackinglog -Server $snServerNameDrop.SelectedItem.ToString() -Start $dtQueryDT -End $dtQueryDTf -ResultSize 10000 | ForEach-Object{
if ($_.EventId -eq "Send" -bor $_.EventId -eq "Receive"){
$saSndArray = $_.Sender.ToString().Split("@")
if ($saSndArray.length -gt 0){
if ($DomainHash.ContainsKey($saSndArray[1].ToLower())){
if ($Senders.ContainsKey($_.Sender.ToString().ToLower()) -eq $false){
$Senders.Add($_.Sender.ToString().ToLower(),$_.Timestamp.ToString())
$SendersSubject.Add($_.Sender.ToString().ToLower(),$_.MessageSubject.ToString())
}
else{
if ([DateTime]::Parse($Senders[$_.Sender.ToString().ToLower()]) -lt $_.Timestamp){
$Senders[$_.Sender.ToString().ToLower()] = $_.Timestamp.ToString()
$SendersSubject[$_.Sender.ToString().ToLower()] = $_.MessageSubject.ToString()
}
}
}
}
foreach ($recp in $_.recipients){
if ($recp -ne ""){
if ($Recipeints.ContainsKey($recp.ToLower()) -eq $false){
$Recipeints.Add($recp.ToLower(),$_.Timestamp.ToString())
$RecipeintsSubject.Add($recp.ToLower(),$_.MessageSubject.ToString())
}
else{
if ([DateTime]::Parse($Recipeints[$recp.ToLower()]) -lt $_.Timestamp){
$Recipeints[$recp.ToLower()] = $_.Timestamp.ToString()
$RecipeintsSubject[$recp.ToLower()] = $_.MessageSubject.ToString()
}
}
}
}


}}
foreach ($key in $Recipeints.keys){
$Recvtimespan = New-TimeSpan -start ($Recipeints[$key]) -end $(Get-Date)
$user = get-user $key
if ($user -ne $null){
if ($Senders.ContainsKey($key)){
$SentTimeSpan = New-TimeSpan -start ($Senders[$key]) -end $(Get-Date)
$ltTable.Rows.Add($user.displayName,$Senders[$key],[math]::round($SentTimeSpan.TotalMinutes,0),$Recipeints[$key],[math]::round($Recvtimespan.TotalMinutes),$RecipeintsSubject[$key],$SendersSubject[$key])}
else {$ltTable.Rows.Add($user.displayName,"n/a","n/a",$Recipeints[$key],[math]::round($Recvtimespan.TotalMinutes,0),$RecipeintsSubject[$key],"n/a")}
}}
foreach ($key in $Senders.keys){
$user = get-user $key
if ($user -ne $null){
if ($Recipeints.ContainsKey($key) -eq $false){
$SentTimeSpan = New-TimeSpan -start ($Senders[$key]) -end $(Get-Date)
$ltTable.Rows.Add($user.displayName,$Senders[$key],[math]::round($SentTimeSpan.TotalMinutes,0),"n/a","n/a","n/a",$SendersSubject[$key])

}}

}
$dgDataGrid.DataSource = $ltTable
}

$ltTable = New-Object System.Data.DataTable
$ltTable.TableName = "SentAndRecv"
$ltTable.Columns.Add("Mailbox")
$ltTable.Columns.Add("Last Mail Sent")
$ltTable.Columns.Add("Minutes since Last Mail Sent")
$ltTable.Columns.Add("Last Recieved Mail")
$ltTable.Columns.Add("Minutes since Last Mail Recieved")
$ltTable.Columns.Add("Subject of Last Mail Recieved")
$ltTable.Columns.Add("Subject of Last Mail Sent")
$form = new-object System.Windows.Forms.form
$form.Text = "Exchange Last Sent and Recieved Form"


# Add Search Button

$exButton = new-object System.Windows.Forms.Button
$exButton.Location = new-object System.Drawing.Size(270,20)
$exButton.Size = new-object System.Drawing.Size(85,20)
$exButton.Text = "Search"
$exButton.Add_Click({GetLogs})
$form.Controls.Add($exButton)

# Add Server DropLable
$snServerNamelableBox = new-object System.Windows.Forms.Label
$snServerNamelableBox.Location = new-object System.Drawing.Size(10,20)
$snServerNamelableBox.size = new-object System.Drawing.Size(100,20)
$snServerNamelableBox.Text = "ServerName"
$form.Controls.Add($snServerNamelableBox)

# Add Server Drop Down
$snServerNameDrop = new-object System.Windows.Forms.ComboBox
$snServerNameDrop.Location = new-object System.Drawing.Size(130,20)
$snServerNameDrop.Size = new-object System.Drawing.Size(130,30)
get-Exchangeserver | ForEach-Object{$snServerNameDrop.Items.Add($_.Name)}
# $snServerNameDrop.Add_SelectedValueChanged({GetLogs})
$form.Controls.Add($snServerNameDrop)
# Add DateTimePickers Button

$dpDatePickerFromlableBox = new-object System.Windows.Forms.Label
$dpDatePickerFromlableBox.Location = new-object System.Drawing.Size(10,50)
$dpDatePickerFromlableBox.size = new-object System.Drawing.Size(90,20)
$dpDatePickerFromlableBox.Text = "Logged Between"
$form.Controls.Add($dpDatePickerFromlableBox)

$dpTimeFrom = new-object System.Windows.Forms.DateTimePicker
$dpTimeFrom.Location = new-object System.Drawing.Size(120,50)
$dpTimeFrom.Size = new-object System.Drawing.Size(190,20)
$form.Controls.Add($dpTimeFrom)

$dpDatePickerFromlableBox1 = new-object System.Windows.Forms.Label
$dpDatePickerFromlableBox1.Location = new-object System.Drawing.Size(10,70)
$dpDatePickerFromlableBox1.size = new-object System.Drawing.Size(50,20)
$dpDatePickerFromlableBox1.Text = "and"
$form.Controls.Add($dpDatePickerFromlableBox1)

$dpTimeFrom1 = new-object System.Windows.Forms.DateTimePicker
$dpTimeFrom1.Location = new-object System.Drawing.Size(120,70)
$dpTimeFrom1.Size = new-object System.Drawing.Size(190,20)
$form.Controls.Add($dpTimeFrom1)

$dpTimeFrom2 = new-object System.Windows.Forms.DateTimePicker
$dpTimeFrom2.Format = "Time"
$dpTimeFrom2.value = [DateTime]::get_Now().AddHours(-1)
$dpTimeFrom2.ShowUpDown = $True
$dpTimeFrom2.Location = new-object System.Drawing.Size(315,50)
$dpTimeFrom2.Size = new-object System.Drawing.Size(90,20)
$form.Controls.Add($dpTimeFrom2)

$dpTimeFrom3 = new-object System.Windows.Forms.DateTimePicker
$dpTimeFrom3.Format = "Time"
$dpTimeFrom3.ShowUpDown = $True
$dpTimeFrom3.Location = new-object System.Drawing.Size(315,70)
$dpTimeFrom3.Size = new-object System.Drawing.Size(90,20)
$form.Controls.Add($dpTimeFrom3)



# Add DataGrid View

$dgDataGrid = new-object System.windows.forms.DataGridView
$dgDataGrid.Location = new-object System.Drawing.Size(10,100)
$dgDataGrid.size = new-object System.Drawing.Size(1000,600)
$dgDataGrid.AutoSizeColumnsMode = "AllCells"

$form.Controls.Add($dgDataGrid)

#populate DataGrid

$form.topmost = $true
$form.Add_Shown({$form.Activate()})
$form.ShowDialog()


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...

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You ...

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why ...
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.