DCSIMG
Tracing commands - Get-CommandTraceSource - Shay Levy

Shay Levy

If you repeat it, PowerShell it!

Tracing commands - Get-CommandTraceSource

Windows PowerShell provides several cmdlets to trace various low-level PowerShell components. When tracing, components generate detailed messages about each step in its internal processing. Developers use the trace data to monitor data flow, program execution, and errors. Tracing is mostly for developers but they are available to all users.

The following shows the output of Trace-Command using its first help code example (for more information about the Trace-Command cmdlet, type: Get-Help Trace-Command -Full):

image

 

Trace-Command expects two pieces of information, one or more trace sources and an expression (enclosed in braces). We can get a list of trace sources by using the Get-TraceSource cmdlet:

PS > Get-TraceSource 

Options Name Listeners Description
------- ---- --------- -----------
None SingleShell {Default} SingleShell
None MshSnapinLoadUnload {Default} Loading and unloading mshsnapins
None MshConsoleInfo {Default} MshConsoleInfo object that is constructed from...
None PSSnapInInfo {Default} PSSnapInInfo
None PSSnapInReader {Default} PSSnapInReader
None RunspaceConfigura... {Default} RunspaceConfigurationEntryCollection
(...)

 

However, sometimes it is not so obvious which source we need to use to trace a specific component (cmdlet). That's why I wrote the Get-CommandTraceSource function. Give it a command name (cmdlet) and Get-CommandTraceSource will get you back the command's PSTraceSource object (if implemented).

#Requires -Version 2.0 

function Get-CommandTraceSource
{
param(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[System.String]$Command
)

process
{
try
{
$c = Get-Command $Command -CommandType Cmdlet
$c.ImplementingType.GetField("tracer","Static,NonPublic").GetValue($null)
}
catch
{
throw
}
}
}



# Example
PS > Get-CommandTraceSource -Command Start-Job

Options Name Listeners Description
------- ---- --------- -----------
None JobCmdlets {Default} Job related cmdlets

 

Here's a one-liner that outputs all cmdlets trace sources:

PS > Get-Command -CommandType Cmdlet | Add-Member ScriptProperty TraceSource {$this.ImplementingType.GetField("tracer","Static,NonPublic").GetValue($null).Name} -PassThru | Where-Object {$_.TraceSource} | Select-Object Name,TraceSource

Name                          TraceSource
---- -----------
Add-Computer AddComputer
Add-Member AddMember
Checkpoint-Computer CheckpointComputer
Clear-EventLog ClearEventLog
Compare-Object CompareObject
ConvertFrom-StringData ConvertFromStringData
ConvertTo-Html ConvertToHtml
Disable-ComputerRestore DisableComputerRestore
Enable-ComputerRestore EnableComputerRestore
Export-Alias AliasCommands
Export-ModuleMember Parser
Export-PSSession ExportPSSessionCommand
ForEach-Object Parser
Get-Acl GetAclCommand
Get-Command GetCommandCmdlet
Get-ComputerRestorePoint GetComputerRestorePoint
Get-Credential CredentialCommands
Get-EventLog GetEventLog
Get-Help GetHelpCommand
Get-HotFix GetHotFix
Get-PfxCertificate CertificateCommands
Get-PSSession GetPSSession
Get-Random GetRandomCommand
Get-Unique GetUnique
Get-WmiObject TransactionCommands
Group-Object GroupObjectCommand
Import-Alias AliasCommands
Import-LocalizedData ImportLocalizedData
Import-PSSession ImportPSSessionCommand
Invoke-Expression InvokeExpressionCommand
Invoke-WmiMethod TransactionCommands
Limit-EventLog LimitEventLog
Measure-Command MeasureCommandCommand
Measure-Object MeasureObject
New-EventLog NewEventLog
New-Object NewObjectCommand
New-PSSession NewPSSession
New-PSSessionOption NewPSSessionOption
New-TimeSpan NewTimeSpanCommand
New-WebServiceProxy StartProcess
Out-Default OutDefaultCommand
Out-File OutFileCommand
Out-GridView format_out_OutGridViewCommand
Out-Host OutHostCommand
Read-Host HostCmdlets
Remove-Computer RemoveComputer
Remove-EventLog RemoveEventLog
Remove-Module Parser
Remove-PSSession RemovePSSessionCommand
Remove-WmiObject TransactionCommands
Reset-ComputerMachinePassword ResetComputerMachinePassword
Restart-Computer RestartComputer
Restore-Computer RestoreComputer
Select-Object SelectObject
Select-String SelectStringCommand
Set-Acl SetAclCommand
Set-Date SetDateCommand
Set-ExecutionPolicy ExecutionPolicyCommands
Set-PSBreakpoint SetPSBreakpoint
Set-PSDebug Parser
Set-WmiInstance TransactionCommands
Show-EventLog ShowEventLogCommand
Sort-Object SortObject
Start-Job JobCmdlets
Start-Process StartProcess
Start-Sleep StartSleepCommand
Start-Transcript StartTranscriptCommand
Stop-Computer StopComputer
Stop-Transcript StopTranscriptCommand
Tee-Object TeeObject
Test-Connection TestConnection
Update-FormatData UpdateFormatDataCommand
Update-TypeData UpdateTypeDataCommand
Where-Object Parser
Write-Debug WriteDebugCommand
Write-EventLog WriteEventLog
Write-Output WriteOutputCommand
Write-Progress WriteProgressCommand
Write-Verbose WriteVerboseCommand
Write-Warning WriteWarningCommand

 

See Also:

Set-TraceSource 

 


 


       

            
    

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: