Wednesday, September 14, 2016

PowerShell v2 Function Limit TraceSource

,
In working with the Trace-Command cmdlet one is almost dependent on the Get-TraceSource cmdlet unless you have a great memory or work with it enough to have the list in the back of you mind. In my case, I got sick of running a few lines every time I wanted to research something in a pinch, so, I wrote this function to do an inclusion of all matching TraceSources with the -Name option, or, an exclusion, which returns all TraceSources that do not match the name(s) passed to the function. Here is the function itself.
function Limit-TraceSource
{
     param(
          [Parameter(
              Mandatory = $false
          )]
          [String[]]
          $Name,
         
          [Parameter(
              Mandatory = $false
          )]
          [Switch]
          $Exclude
     )
    
     if($Exclude)
     {
          Get-TraceSource | Where { $_.name -notmatch "($($Name -join |))"}
     }
     else
     {
          Get-TraceSource ($Name | % { "*$_*" } )
     }
}
And here is a quick usage of the inclusive approach. When using this pattern, type in the full or partial name(s) of a TraceSource with which you would like to work.  This one returns all TraceSources whose names match Cmdlet:
[array] $names =Limit-TraceSources -Name Cmdlet | % { $_.name }
This command will output this:

$names
CmdletConfigurationEntry
CmdletProviderAttribute
CmdletProviderClasses
CmdletProviderContext
Cmdlet
GetCommandCmdlet
CmdletProviderIntrinsics
ScriptAsCmdlet
CmdletInfo

You want to use an array, thats fine:
[array] $names =Limit-TraceSource -Name Cmdlet,bind | % { $_.name }
Which outputs
$names
CmdletConfigurationEntry
CmdletProviderAttribute
CmdletProviderClasses
CmdletProviderContext
Cmdlet
GetCommandCmdlet
CmdletProviderIntrinsics
ScriptAsCmdlet
CmdletInfo
ParameterBinderBase
ParameterBinding
ReflectionParameterBinder
ParameterBinderController
ParameterBindingException
ScriptParameterBinder
FormatViewBinding
In usage, it looks like this:
Trace-Command -Name $names -Expression { list } –PSHost 
Or, if you want to be more terse with a one-liner,
 Trace-Command -Name @(Limit-TraceSource -Name Cmdlet | % { $_.name }) -Expression { list } -PSHost
Here is the exclusion patterns:
[array] $names =Limit-TraceSource -Name Console,parameter -Exclude | % { $_.name }
which yields a much longer list...but, one free of any Console or Parameter TraceSources.
$names
SingleShell
MshSnapinLoadUnload
PSSnapInInfo
PSSnapInReader
RunspaceConfigurationEntryCollection
RunspaceConfigurationEntry
CmdletConfigurationEntry
AssemblyConfigurationEntry
RunspaceConfiguration
RunspaceInit
CmdletProviderAttribute
ProviderConfigurationEntry
ResourceManagerCache
LocalRunspace
UniversalResourceName
ExecutionContext
AuthorizationManager
PSAuthorizationManager
InternalHost
InternalHostRawUserInterface
SessionState
SessionStateScope
Runspace
ETS
TypeConversion
Parser
Tokenizer
DetailedCommandDiscovery
CommandDiscovery
TypeTable
DeserializingTypeConverter
TypeInfoDataBaseManager
TypeInfoDataBaseLoader
XmlLoaderBase
FormatFileLoading
State
CoreCommandProvider
PSCredential
PSDriveInfo
CmdletProviderClasses
PSTransaction
CmdletProviderContext
MshLog
EventLogLogProvider
AliasProvider
EnvironmentProvider
FileSystemProvider
VariableProvider
RegistryProvider
CertificateProvider
X509StoreLocation
DriveCommandAPI
LocationGlobber
PathResolution
ProviderCommandAPI
WildcardPattern
PathInfo
CommandFactory
History
InitialSessionState
CommandMetadata
CompiledCommandAttribute
MemberResolution
PSSnapInLoadUnload
HostUtilities
Executor
PipelineStateInfo
Pipeline
PipelineBase
ObjectStream
ObjectWriter
LocalPipeline
InternalCommand
ScriptCommandProcessor
CommandSearch
CommandProcessor
OutDefaultCommand
Cmdlet
PipelineProcessor
CommandProcessorBase
TerminatingErrorContext
WriteLineHelper
format_out_OutputManagerInner
format_out_FrontEndCommandBase
format_out_ImplementationCommandBase
SecuritySupport
ExternalScriptInfo
WriteOutputCommand
format_out_CommandWrapper
OutLineOutputCommand
format_out_OutCommandInner
FormatObjectDeserializer
Deserializer
PSVariableCommandAPI
FormatInfoDataClassFactory
PathCommandAPI
SessionStateException
ProcessCommands
NavigationCommands
GetCommandCmdlet
CmdletProviderIntrinsics
ProviderIntrinsics
WriteVerboseCommand
ScriptAsCmdlet
CmdletInfo
WriteOrThrowErrorCommand
ErrorRecord
StreamingTextWriter
ErrorCategoryInfo
CompareObject
SelectObject
OrderByProperty
ObjectCommandComparer
WriteDebugCommand
FormatViewBinding
DisplayDataQuery
TypeMatch
Write-Debug
NewObjectCommand
FunctionInfo
ScriptBlock
SessionStateProvider
CredentialAttribute
StartTranscriptCommand
AliasCommands
CommandNotFoundException
CommandCompletion
So, next time you want to research something and would rather not watch your console go nuts, use

0 comments to “PowerShell v2 Function Limit TraceSource”

Post a Comment

 

Computer Info Copyright © 2016 -- Powered by Blogger