Skip to main content

Power-Shelling the Message Tracking logs on Exchange 2000/2003

If you’ve ever had to write any scripts that query the message tracking logs though WMI you may have come across the issue that WQL cant handle doing a Where clause on an array. Because the Recipient address’s on an email are returned as an array when you want to use this class to make a query that only shows mail that is going to one address it can be a little cumbersome. With Powershell although you are still stuck not being able to filter via recipient address in a WMI query what you can do now is pipe that result to the where-object cmdlet where you can then filter the result to one particular recipient address.

If you combine this in a script what you can then do is query the tracking logs for the past x number of hours to show any email sent to a particular recipient. The reverse is also true you can query to see all the email sent from a particular address.

What I’ve done is put two scripts together that do this both scripts take 3 input parameters the first is the name of the server you want to query. The second is the number of hours to look back in the logs and the third is the email address to look at so to run the script you use a line such as c:\recvmail.ps1 servername 10 user@domain.com . This would look for any mail sent to user@domain.com on the server for the last 10 hours. The sent mail script works that same but shows messages that where sent from a particular email address The script requires that your running the Powershell RC but it should work in the old msh / monad beta’s as well.

I’ve put a downloadable copy of the script here

The Received email script looks like

param([String] $servername = $(throw "Please specify the Servername"), [int32] $timerange = $(throw "Please specify a Time Range in Hours"),[String] $emailaddress= $(throw "Please Specify the Email address you wish to use"))
$dtQueryDT = [DateTime]::UtcNow.AddHours(-$timerange)
$WmidtQueryDT = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime($dtQueryDT)

$WmiNamespace = "ROOT\MicrosoftExchangev2"
$filter = "entrytype = '1020' and OriginationTime >= '" + $WmidtQueryDT + "' or entrytype = '1028' and OriginationTime >= '" + $WmidtQueryDT + "'"
get-wmiobject -class Exchange_MessageTrackingEntry -Namespace $WmiNamespace -ComputerName $servername -filter $filter | where-object {$_.RecipientAddress -eq $emailaddress} | where-object {$_.SenderAddress -ne "<>"} | ft @{expression={[System.Management.ManagementDateTimeConverter]::ToDateTime($_.OriginationTime)}; width=25; label="Time Sent"},SenderAddress,Subject

The sent Email script looks like

param([String] $servername = $(throw "Please specify the Servername"), [int32] $timerange = $(throw "Please specify a Time Range in Hours"),[String] $emailaddress= $(throw "Please Specify the Email address you wish to use"))
$dtQueryDT = [DateTime]::UtcNow.AddHours(-$timerange)
$WmidtQueryDT = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime($dtQueryDT)
$WmiNamespace = "ROOT\MicrosoftExchangev2"
$filter = "entrytype = '1020' and OriginationTime >= '" + $WmidtQueryDT + "' and SenderAddress = '" + $emailaddress + "' or entrytype = '1028' and OriginationTime >= '" + $WmidtQueryDT + "' and SenderAddress = '" + $emailaddress + "'"
get-wmiobject -class Exchange_MessageTrackingEntry -Namespace $WmiNamespace -ComputerName $servername -filter $filter | ft @{expression={[System.Management.ManagementDateTimeConverter]::ToDateTime($_.OriginationTime)}; width=25; label="Time Sent"},@{expression={$_.RecipientCount};width=5;label="#Recp"},RecipientAddress,Subject

Popular posts from this blog

Export calendar Items to a CSV file using EWS and Powershell

Somebody asked about this last week and while I have a lot of EWS scripts that do access the Calendar I didn't have a simple example that just exported a list of the Calendar events with relevant information to a CSV file so here it is.

I've talked on this one before in this howto  but when you query the calendar folder using EWS you need to use a CalendarView which will expand any recurring appointments in a calendar. There are some limits when you use a calendarview in that you can only return a maximum of 2 years of appointments at a time and paging will limit the max number of items to 1000 per call. So if you have a calendar with a very large number of appointments you need to break your query into small date time blocks. In this example script I'm just grabbing the next 7 days of appointments if you want to query a longer period you need to adjust the following lines (keeping in mind what I just mentioned)

#Define Date to Query
$StartDate = (Get-Date)
$EndDate = (Ge…

EWS Managed API and Powershell How-To series Part 1

I thought I'd start the year with a series of posts that goes back over the basics of using the EWS Managed API from Powershell and provides a modular remarked example that you can easily cut and paste to build your own scripts. Along the way in this series I'll show a whole bunch of examples around specific things.

As a starting point for versions this will be Powershell Version 2.0  and the EWS Managed API 1.1 (which will soon change to 1.2 once released) http://www.microsoft.com/download/en/details.aspx?id=13480.

The starting point for any EWS script your going to write is connecting to Exchange for which there are three important pieces of information you will need. Firstly you need to know the version of Exchange your running in this script its going to be held in the following variable

$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1

Other valid values for Exchange 2007 would be

$ExchangeVersion = [Microsoft.Exchange.WebServices.…

Writing a simple scripted process to download attachmentts in Exchange 2007/ 2010 using the EWS Managed API

Every complicated thing in life is made up of smaller simpler building blocks, when it comes to writing a script (or any code really) the more of these little building blocks you have to figure out the more the process of solving a problem can become bewildering. The Internet generally provides you with lots of half eaten sandwiches of information something someone else has taken a bite out but a lot of the time half done, and as with any code its usefulness declines over time as new and better API's and methods are derived. In this post I'm going to go through a simple scripted process that hopefully covers a few more of these smaller building blocks that you might face when asked to come up with a simple costless solution to perform an automated business function with a script.

So the process im going to look at is one that comes up a lot and that is you have an Email that comes into to certain mailbox every day with a certain subject in my case "Daily Export" this …
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.