Skip to content

Instantly share code, notes, and snippets.

@alx9r
Created December 12, 2024 23:37
Show Gist options
  • Save alx9r/f818d675757933e8cc45b94101ccc9bd to your computer and use it in GitHub Desktop.
Save alx9r/f818d675757933e8cc45b94101ccc9bd to your computer and use it in GitHub Desktop.
Cmdlet for tracing invocations by the PowerShell engine internals
using System;
using System.Management.Automation;
using System.Collections.Concurrent;
[Cmdlet(VerbsDiagnostic.Trace,"CmdletInternals")]
public class TraceCmdletInternalsCommand : Cmdlet, IDisposable {
public static ConcurrentQueue<string> Log { get; set; }
static TraceCmdletInternalsCommand() {
Log = new ConcurrentQueue<string>();
}
object _inputObject;
[Parameter(ValueFromPipeline=true)]
public object InputObject {
set {
Log.Enqueue(Name+" set InputObject '"+value+"'");
_inputObject = value;
}
get {
return _inputObject;
}
}
[Parameter()]
public string Name {get;set;}
object _parameter;
[Parameter()]
public object Parameter {
set {
Log.Enqueue("Set Parameter '"+value+"'");
_parameter = value;
}
get {
return _parameter;
}
}
[Parameter()]
public SwitchParameter PassThru { get; set; }
public TraceCmdletInternalsCommand() {
Name = "";
logMethod("TraceCmdletInternalsCommand()");
}
void logMethod(string methodName) {
Log.Enqueue(string.Join(" ",Name,methodName,"Stopping: "+Stopping.ToString()));
}
protected override void BeginProcessing() {
logMethod("BeginProcessing()");
}
protected override void ProcessRecord() {
logMethod("ProcessRecord()");
if (PassThru.IsPresent) {
WriteObject(InputObject);
}
}
protected override void EndProcessing() {
logMethod("EndProcessing()");
}
protected override void StopProcessing() {
logMethod("StopProcessing()");
}
public void Dispose(){
logMethod("Dispose()");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment