Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

PowerShell cmdlets in C# - usage of getDynamicParameters() - Looking for help

$
0
0

 

Apologies for the lengthy read.  I look for some help and hence choosen to drop the code-snippet here.  Need this to help with my work at office.

wrote a cmdlet in C# to accept 2 parameters A and B .  Each has 2 dependent

parameters.   Both A and B are optional.

Defined 2 classes to provide the definition of the dependent parameters.

Coded getDynamicParameters() to check for A and B and return instances of the respective classes.

When executed,  the optional parameters do get expanded but the cmdlet

throws an error that the dependent parameters are missing.  

Any clues ? Is this the right approach ? 

Is there a way to get the command parameter for which getDynamicParameters is called

so that I can respond  with the appropriate class of dependent parameters from

getDynamicParameters?

 Is it that getDynamicParameters() is called only once ?  I would want 

it to be called multiple times so that I can meet my requirement mentioned

above.   

Would it be called when we try to expand the commands using tabs ? Or after

the command is executed ?

 

PS C:\Users\Administrator> test-Command -ParamB -GroupName1 g1 -Type1 t1 -ParamA -GroupName g -Type t
test-Command : A parameter cannot be found that matches parameter name'GroupName1'.
At line:1 char:22
test-Command -ParamB -GroupName1 g1 -Type1 t1 -ParamA -GroupName g -Type t
                    ~~~~~~~~~~~
CategoryInfo          : InvalidArgument: (:) [test-Command], ParameterBindingException

 

namespace testcmd

{
    [Cmdlet("test", "Command")]
    public class testCmdLet : Cmdlet, IDynamicParameters
    {

 

       [Parameter]
        public SwitchParameter ParamA
        {
            get { return paramA; }
            set { paramA = value; }
        }
            private Boolean paramA;


            [Parameter]
            public SwitchParameter ParamB
            {
                get { return paramB; }
                set { paramB = value; }
            }
            private Boolean paramB;


        private DynamicParameters dynParameters;
        private DynamicParameters1 dynParameters1;

        public object GetDynamicParameters()
        {

            count++;   
            if (paramA)
            {
                dynParameters = new DynamicParameters();
                return dynParameters;
            }
            if (paramB)
            {
                dynParameters1 = new DynamicParameters1();
                return dynParameters1;

            }
            return null;
        }

        public class DynamicParameters {
            [Parameter]
            public string GroupName
            {
                get { return groupName; }
                set { groupName = value; }
            }
            private string groupName;
        }


        public class DynamicParameters1
        {
            [Parameter]
            public string GroupName1
            {
                get { return groupName1; }
                set { groupName1 = value; }
            }
            private string groupName1;

        }

        protected override void ProcessRecord()
        {
            WriteObject("count  " + count);
            if (paramA)
            {
                WriteObject("nic1 + " + dynParameters.GroupName + " type " + dynParameters.Type +" ct " + count);

            }

            if (paramB)
            {
                WriteObject("nic2 + " + dynParameters1.GroupName1 + " type " + dynParameters1.Type1 + " ct " + count);
            }
        }
    }
}


Viewing all articles
Browse latest Browse all 6937

Trending Articles