This site uses advanced css techniques
In a previous cmdletter I recommended creating a base class for all your cmdlets to hold common processing, and I have been using this more and more.
Error reporting/handling is a common use case, and one part of this can be reporting the name of the cmdlet throwing the error. For a time I simply passed a string to whereever needed this (perhaps to a WriteVerbose(), but finally tripped across the simple built-in way to do this, which I've put inside the base class:
public abstract class MyCmdlet : PSCmdlet { protected string CmdletName => this.MyInvocation.InvocationName; // more helper code later }
Now, some other helper code in the same base class, called by multiple cmdlets, can simply refer to CmdletName and reliably report which cmdlet in a pipeline is providing the message.
The CmdletName property is not available from the base Cmdlet class, only PSCmdlet.
First published: 2019/07/05