A few months ago I was working on collecting some statistics out of our Exchange environment. I wanted to take a few samples of user specific message traffic to help with some sizing calculations and just do a sanity check on our existing usage. I know there are other tools and utilities that exist, but I wanted try to write my own. As a result, I added and defined the following requirements for this particular script:
- Hand it over to a Helpdesk Analyst and have the output make sense to them
- Consume the script logic into a much larger health and status check on a per user basis script.
- Make it possible for the scripts executor to have no knowledge of the Exchange environment other than having the Exchange Management tools installed.
A few challenges arose during this process:
- A multi-site Exchange environment requires Site & Hub Transport discovery of the site that the mailbox is homed. If you were a Helpdesk Analyst you most likely wouldn’t know this information.
- There could be multiple Hub Transport servers in a site and message tracking events could exist on both.
- Performance hit on the Hub Transport servers in the event 30 days was opt’d for.
I’m not going to include the entire script in the body of this post, you can download it at the bottom, but I want to call out a few sections:
The logic behind the Site & Hub Transport discovery
Here is the cross selection of the script that requests the SMTP Address of the user who you want to report against. From here we determine the Mailbox server and correlating AD Site. Once we’ve determined the site we can then grab a list of Hub Transport servers to query against.
$user = Read-Host "What is the users email address?"
$MbxServer = (get-mailbox $user).Servername
$Site = (Get-exchangeserver $MbxServer).site.name
$SiteHTServers = Get-Exchangeserver | Where {($_.IsHubTransportServer -eq $true) -and ($_.site.name -like "$Site")}
The second and last second piece of logic I wanted to focus on is the use of a Switch statement which was new to me. I’ve used Case statements in VB programming in my past, so it was relatively easy to understand. Essentially we are defining variables based the $DateInput variable which is the value the user enters interactively for the number of days.
switch ($DateInput)
{
1 {
$StartDate = $today
$Days = 1
$SizeFactor = "1KB"
$SizeLabel = "KB"
break;
}
2 {
$StartDate = (get-date).adddays(-7).ToString("MM/dd/yyyy")
$Days = 7
$SizeFactor = "1MB"
$SizeLabel = "MB"
break;
}
3 {
$StartDate = (get-date).adddays(-14).ToString("MM/dd/yyyy")
$Days = 14
$SizeFactor = "1MB"
$SizeLabel = "MB"
break;
}
4 {
$StartDate = (get-date).adddays(-30).ToString("MM/dd/yyyy")
$Days = 30
$SizeFactor = "1GB"
$SizeLabel = "GB"
break;
}
default {
"** The selection could not be determined **";
break;
}
}
You can download the full script here: get-messagetracking.txt