Question

I have developed a custom c# cmdlet. It has three parameters (all of them are strings) and none of them are mandatory. Two of them belong to parameterset1 and the third belongs to parameterset2. It is working fine, when a user gives parameters from both the parameter sets then it gives message that they do not belong to same parameter set. But I'm getting two problems:

  1. When user doesn't give any parameter(because none of them are mandatory) then it throws the error Parameter set cannot be resolved using the specified named parameter
  2. When the user give first parameter and type - and then hitting tab should only show second parameter because first and second are in same parameter set but it is showing 2nd and 3rd which is not correct. Likewise it is showing 1st and 2nd even the user enter 3rd parameter first.

Here is the code how I defined the parameters:

    [System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get, "Customcmd")]
    public class Get_Customcmd: System.Management.Automation.PSCmdlet
    {
        [System.Management.Automation.Parameter(Mandatory = false, ParameterSetName = "Set1")]
        public string Param1;
        [System.Management.Automation.Parameter(Mandatory = false, ParameterSetName = "Set1")]
        public string Param2;
        [System.Management.Automation.Parameter(Mandatory = false, ParameterSetName = "Set2")]
        public string Param2;
        protected override void ProcessRecord()
        {

Can anyone tell me did I miss anything? Should add anymore attributes to the parameters?

Was it helpful?

Solution

  1. Since you are using only named parameters, you need to either mark one of them as 'DefaultParameterSet' like

    [Cmdlet(VerbsCommon.New, "Customcmd", DefaultParameterSetName = Set1)]

    or have at least one parameter which is not a part of any named ParameterSet.

  2. PS doesn't check whether the tab suggestions belong to same parameter set or not. So you are not doing anything wrong here, thats the way PS behaves.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top