For example you have a requirement where you are writing
custom cmdlet cmdLET1 which in turn needs to invoke cmdLET2.In
such a scenario the challenge remains how to pass the parameters to the second
cmdlet being invoked and also how exactly to invoke it.
Shown below is the sample code which shows how do we achieve
such a scnerio
PowerShell pShell = PowerShell.Create();
Runspace
runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
Pipeline
pipeline = Runspace.DefaultRunspace.CreateNestedPipeline();
Command
cmd = new Command("Add- cmdLET2");
CommandParameter cmdPara1 = new CommandParameter("parameter1", value1);
cmd.Parameters.Add(cmdPara1);
CommandParameter cmdPara2 = new CommandParameter("parameter2", value2);
cmd.Parameters.Add(cmdPara2);
pipeline.Commands.Add(cmd);
pShell.Commands.AddCommand("import-module").AddParameter("Name", "Add- cmdLET2");
pipeline.Invoke();
The above code when called from cmdLET1 will invoke cmdLET2
with values passed in the CommandParameter’s
No comments:
Post a Comment