Exchange 2007 MessageTracking

So as you may know, MessageTracking in Exchange 2007 is pants. 

Out of the box you can only track a message from one server and have to manually track messages from one server until the next.  The default MessageTracking doesn’t even let you do this.

So, this is one I made earlier ;-) A little PowerShell Script that requires a starting server and a message ID.

It will then works out where the next server is and continue the message track.

Here is the script, let me know what you think and if it does what you expect. I have looked at trying to track in a mixed 2003/2007 mode, but the message tracking id are different, so it’s not going to be possible :-(

What I normally do is use the default message tracking to get the messageid and then use my script to do a “proper” track.

If you don’t supply a starting server or messageid, it will fire up a GUI.

Enjoy

 

#############################################
$AppName = “get-MsgTrack.ps1”
$AppVer = “v1.1 [26 June 2007]”
#v1.0 5 May 2007 #
#v1.1 Added cmdline & GUI #
#Written By Paul Flaherty #
#blogs.flaphead.com #
#############################################

#Variables
#$StartServer = “HubServer”
#$msgID = “”
$MaxHops = 99
$EndNow = $False
$Server = $StartServer
$TrackMsg = $True
$Verbose = $False

#Display script name and version
Write-host $AppName -NoNewLine
Write-Host “: ” $AppVer

#Load Required Assemblies
$null=[reflection.assembly]::LoadWithPartialName(“System.Windows.Forms”)
$null=[reflection.assembly]::LoadWithPartialName(“System.Drawing”)

#Do this when you click on the TrackMsg button
function closeMyDialog
{
$olButton.Text = “Tracking..”
IF ($tboxMsgID.Text.Length -ne 0)
{
$script:msgID = $tboxMsgID.text
}
ELSE
{
Write-Host “GUI MessageID is empty”
$script:TrackMsg = $False
}

IF ($tboxServer.Text.Length -ne 0)
{
$script:StartServer = $tboxServer.Text
}
ELSE
{
Write-Host “GUI Server Name is empty”
$script:TrackMsg = $False
}

if ($verbose) {Write-Host “TrackMsg Button Selected”}
$form.close()
}

#Do this if when you click on the Cancel Button
function cancelMyDialog{
$btnCanx.Text = “Closing..”
$script:TrackMsg = $False
if ($verbose) {Write-Host “Cancel Button Selected”}
$form.close()
}

#Create GUI
Function DisplayGUI
{
$form = new-object System.Windows.Forms.form
$form.Text = $appname + ” ” + $appver

$tboxServer = New-Object System.Windows.Forms.TextBox
$tboxServer.Location = New-Object System.Drawing.Size(15,15)
$tboxServer.Size = New-Object System.Drawing.Size(260,33)
$tboxServer.Text = $Server
$Form.Controls.Add($tboxServer)

$gboxServer = New-Object System.Windows.Forms.GroupBox
$gboxServer.Location = New-Object System.Drawing.Size(12,0)
$gboxServer.Size = New-Object System.Drawing.Size(270,45)
$gboxServer.Text = “Server:”
$Form.controls.add($gboxServer)

$tboxMsgID = New-Object System.Windows.Forms.TextBox
$tboxMsgID.Location = New-Object System.Drawing.Size(15,65)
$tboxMsgID.Size = New-Object System.Drawing.Size(260,50)
$tboxMsgID.Text = $msgID
$tboxMsgID.Multiline = $True
$tboxMsgID.WordWrap = $True
$Form.Controls.Add($tboxMsgID)

$gboxMsgID = New-Object System.Windows.Forms.GroupBox
$gboxMsgID.Location = New-Object System.Drawing.Size(12,50)
$gboxMsgID.Size = New-Object System.Drawing.Size(270,70)

$gboxMsgID.Text = “MessageID:”
$Form.controls.add($gboxMsgID)

$olButton = new-object System.Windows.Forms.Button
$olButton.Location = New-Object System.Drawing.Size(100,130)
$olButton.Size = New-Object System.Drawing.Size(75,23)
$olButton.Text = “TrackMsg”
$olButton.Add_Click({closeMyDialog})
$Form.Controls.Add($olButton)
$form.AcceptButton = $olButton

$btnCanx = new-object System.Windows.Forms.Button
$btnCanx.Location = New-Object System.Drawing.Size(200,130)
$btnCanx.Size = New-Object System.Drawing.Size(75,23)
$btnCanx.Text = “Cancel”
$btnCanx.add_Click({cancelMyDialog})
$Form.Controls.Add($btnCanx)
$form.CancelButton = $btnCanx

$Form.Size = New-Object System.Drawing.Size(300, 190)
$Form.TopMost = $True
$form.Add_Shown({$form.Activate()})

$form.ShowDialog()
}

#I know this is bad, but the DNS lookup WILL fail at the end!
$erroractionpreference = “SilentlyContinue”

#Check to see if verbose is the only commandline options
IF ($args[0] -eq “-verbose”) {$verbose=$True}

#pass in the MessageID and Starting Server Name
#Optional is Verbose
#Need the two variables. If the count is equal to 0 show the GUI
IF ($args.Length -eq 0)
{
IF ($verbose) {Write-Host “No Commandline Entered”}
IF ($verbose) {Write-Host “Showing GUI”}
$ShowGUI = DisplayGUI
}

ELSE
#Gather the commandline
{
IF ($verbose) {Write-Host “Number of Args= ” $args.length}

#Make the assumption that the arguments are in pairs
for($i = 0; $i -le $args.Length; $i++)
{
switch ($args[$i])
{
“-verbose”
{
$verbose = $true
}
“-server”
{
$StartServer = $args[$i+1]
IF ($verbose) {Write-Host “StartServer= ” $StartServer}
$i++
}
“-msgid”
{
$msgID = $args[$i+1]
IF ($verbose) {write-host “MsgId= ” $msgID}
$i++
}
} #Switch
} #For loop
}

#If -verbose only show the GUI
IF ($args.length -eq 1) {$ShowGUI = DisplayGUI}

IF ($verbose) {Write-Host “TrackMsg=” $TrackMsg}

#IF TrackMsg is true then track the message
IF ($TrackMsg -eq $True)
{
$Server = $StartServer
Write-Host “Messaging Tracking”
Write-Host “Starting on: ” -NoNewLine
Write-Host $StartServer
Write-Host $msgID
$i=0

#Loop for the MaxHops
for ($a = 1; $a -le $MaxHops; $a++)
{
$i++
#Pipe MessageTrack to Variable
$StepA = get-messagetrackinglog -Server $server -MessageID $msgid

#Format Text String
$txt1 = “HOP ” + $i + ” [” + $Server + “] to [“

#Set NextIP to $Nul
$NextIP = $Nul

# Get the IP Address of the SEND entry
$StepB = get-messagetrackinglog -Server $server -MessageID $msgid -EventId “SEND”

#Get the Host IP Address from ther “SEND” entry
$StepB_IPAddress = $StepB.ServerIp

#Use .net call to get the Hostname from an IP.
#This can take
a bit of time to do, like a couple of seconds
#This command will fail to set NextServer if it can’t resolve the IP
$NextServer = [System.Net.Dns]::GetHostbyAddress($StepB_IPAddress)
$Server = $NextServer.HostName

#If the NextServer is empty then bail
IF ($NextServer -eq $nul)
{
$Server = $StepB_IPAddress
$EndNow = $True

}

#Write out the message tracking details
Write-Host ” “
$txt1 = $txt1 + $Server + “]”
Write-Host $txt1
$StepA
$NextServer = $Nul

#Bail out of the loop
IF ($EndNow -eq $True){Break}
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.