Format MessageTraceDetail in Exchange Online

Over the last weeks, I had to perform more than usual message traces in Exchange Online. For more details how to run such traces, Tony recently updated his article:

Exchange Online Message Traces are Different to On-Premises Searches (practical365.com)

However, the details you want to look at are not really formatted in a readable format.

What’s the issue?

When you make use of the Cmdlet Get-MessageTraceDetail, you will receive the most information Exchange Online can provide to you right away for messages not older than 10 days. For more details and older messages, you need to run Start-HistoricalSearch, which runs asynchronous (you need to wait for result!).

The details I’m looking for can be found in the property Data, but it’s not formatted:

Solution

Maybe you realized that the property looks like XML and indeed, we can use this and get it formatted the right way. Here is my code:

function Format-MessageTraceDetail
{
    Param (
        [Parameter( Mandatory=$true, Position=0)]
        [System.Object[]]
        $MessageTraceDetail
    )

    foreach ($trace in $MessageTraceDetail)
    {
        # initiate custom objects
         $detailData = New-Object -TypeName psobject
         $detailObject = New-Object -TypeName psobject
         $detailObject | Add-Member -MemberType NoteProperty -Name Date -Value $trace.Date
         $detailObject | Add-Member -MemberType NoteProperty -Name Event -Value $trace.Event
         $detailObject | Add-Member -MemberType NoteProperty -Name Action -Value $trace.Action
         $detailObject | Add-Member -MemberType NoteProperty -Name MessageId -Value $trace.MessageId
         $detailObject | Add-Member -MemberType NoteProperty -Name MessageTraceId -Value $trace.MessageTraceId

        # create XML from data
        [System.Xml.XmlDocument]$xmlData = ''
        $xmlData = $trace.Data
        # get all attributes and add to custom object
        foreach ($node in $xmlData.root.MEP)
        {
            $detailData | Add-Member -MemberType NoteProperty -Name $($node.get_attributes().'#text'[0]) -Value $($node.get_attributes().'#text'[1])
        }

        $detailObject | Add-Member -MemberType NoteProperty -Name Data -Value $detailData
        $detailObject
    }
}

Now you can run your trace, store it in a variable and get it formatted into a more readable format:

This function can be found in my GitHub repository here: Miscellaneous/HelperFunctions.ps1 at master · IngoGege/Miscellaneous · GitHub

Any changes or updates can be found there.

Conclusion

This is nothing “huge”, but can be very handy. Keep in mind to customize your PowerShell with your own functions to improve your efficiency and reduce workload!

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