Share via


Script: Enable and collect ExTRA tracing across all Exchange servers

Here is a PowerShell script that can be used to kick off an ExTRA trace on every Exchange server in the org and collect the traces into a single location. I've highlighted in red the variables you'll likely want to edit prior to running the script.

 # Description:
#   Initiates an ExTRA trace on all the Exchange 2013 servers
#   in an environment and collects the results into a single location.
# Use:
#   In an Exchange Management Shell (EMS), run either of these commands:
#     .\extraTrace.ps1 -Start
#     .\extraTrace.ps1 -Stop
# Author:
#   mahuynh@microsoft.com
# Last modified:
#   2016-06-28

[CmdletBinding()]
Param(
 [switch]$Start,
 [switch]$Stop
)

# check that user ran with either Start or Stop switch params
if (($Start -and $Stop) -or (-not $Start -and -not $Stop)) {
 Write-Error "Please specify only 1 parameter: -Start or -Stop."
 exit
}

$traceConfigFilepath = "C:\EnabledTraces.Config"
$script:nl = "`r`n"
# network path to save the resulting traces (default is local c:\temp\extra)
$TRACES_FILEPATH = "\\" + (hostname) + "\c$\temp\extra"

function CreateExtraTraceConfig
{
 new-item -path $traceConfigFilepath -type file -force
 $string = "TraceLevels:Debug,Warning,Error,Fatal,Info,Performance,Function,Pfd" + $nl
 $string += "MSExchangeWebServices:AllRequests,AllResponses,SubscribeCall,Subscriptions,UnsubscribeCall" + $nl
 $string += "FilteredTracing:No" + $nl
 $string += "InMemoryTracing:No" + $nl
 $string | Out-File -filepath $traceConfigFilepath -Encoding ASCII -Append
}

# main script start

# get a list of all the Exchange servers
$servers = Get-ExchangeServer

if ($Stop) {
 Get-Date
 Write-Host "Stopping ExTRA traces..." -ForegroundColor Cyan $nl

# create target path if it does not exist yet
 if (-not (Test-Path $TRACES_FILEPATH)) {
 mkdir $TRACES_FILEPATH | Out-Null
 Get-Date
 Write-Host "Created $TRACES_FILEPATH as it did not exist yet" -ForegroundColor Green $nl
 }

# stop and delete the trace configs, collect the results
 foreach ($serv in $servers) {
 Get-Date
 Write-Host "Disabling ExTRA tracing on" ($serv.Name) -ForegroundColor green $nl
 logman stop ExchangeDebugTraces -s $serv.Name
 logman delete ExchangeDebugTraces -s $serv.Name
 $fileToMovePath = "\\" + $serv.Name + "\c$\tracing\" + $serv.Name + "_*.etl"
 Move-Item $fileToMovePath $TRACES_FILEPATH -Force
 }

Get-Date
 Write-Host "Finished copying data to" $TRACES_FILEPATH -ForegroundColor Green $nl

} elseif ($Start) {
 Get-Date
 Write-Host "Starting ExTRA traces..." -ForegroundColor Cyan
 
 # create a local extra config file
 Get-Date
 Write-host "Creating local ExTRA trace..." -foregroundcolor Green $nl
 CreateExtraTraceConfig

# for each server
 foreach ($serv in $servers) {
 # copy the extra config file as long as it's not the local computer
 Get-Date
 Write-Host "Enabling ExTRA tracing on" ($serv.Name) -ForegroundColor green $nl
 if ((hostname).ToString().CompareTo($serv.Name) -ne 0) {
 Write-Debug "copying config file..."
 Copy-Item $traceConfigFilepath ("\\" + $serv.Name + "\c$\EnabledTraces.config") -Force
 }

# create a trace
 $outputFilepath = "c:\tracing\" + $serv.Name + ".etl"
 logman create trace ExchangeDebugTraces -p "Microsoft Exchange Server 2010" -o $outputFilepath -s $serv.Name -ow -f bincirc -max 1024

# start the trace
 logman start ExchangeDebugTraces -s $serv.Name
 }
}