Is it possible to share properties and comments between Powershell cmdlets in c#? Is it possible to share properties and comments between Powershell cmdlets in c#? powershell powershell

Is it possible to share properties and comments between Powershell cmdlets in c#?


Yes, there is a way to do this without inheriting from a common base class for the parameters. Its not not well documented, only hinted at in the IDynamicParameters.GetDynamicParameters method's remarks. Here's a more detailed treatment of the topic.

First, create a class with your common parameters declared as properties with the [Parameter] attributes:

internal class MyCommonParmeters{    [Parameter]     public string Foo { get; set; }    [Parameter]    public int Bar { get; set; }    ...}

Then each Cmdlet that wants to use these common parameters should implement the IDynamicParameters interface to return a member instance of the MyCommonParameters class:

[Cmdlet(VerbsCommon.Add, "Froz")]public class AddFroz : PSCmdlet, IDynamicParameters{    private MyCommonParmeters MyCommonParameters         = new MyCommonParmeters();    object IDynamicParameters.GetDynamicParameters()    {        return this.MyCommonParameters;    }    ...

With this approach, the PowerShell command parameter binder will find and populate the parameters on the MyCommonParameters instance just as if they were members of the Cmdlet classes.