2

How can I run precursory commands such as set-adserversettings when I invoke a powershell command in C#? Right now it is returning 0 results.

Here is the code I am using:

Command command1 = new Command("set-adserversettings");
CommandParameter parameter1 = new CommandParameter("viewentireforest", true);
command1.Parameters.Add(parameter1);

Command command2 = new Command("set-userphoto");
CommandParameter parameter2a = new CommandParameter("identity", tbxName.Text);
CommandParameter parameter2b = new CommandParameter("picturedata", displayedImage);
CommandParameter parameter2c = new CommandParameter("domaincontroller", "xx-xx-xx-01.xx.xx.xx.xxx");
CommandParameter parameter2d = new CommandParameter("confirm", false);
command2.Parameters.Add(parameter2a);
command2.Parameters.Add(parameter2b);
command2.Parameters.Add(parameter2c);
command2.Parameters.Add(parameter2d);

Pipeline pipeline = runspacee.CreatePipeline();

pipeline.Commands.Add(command1);
pipeline.Commands.Add(command2);

var exResults = pipeline.Invoke();
1
  • What are you able to do in PoSh that you are not able to do using .Net? Commented Feb 21, 2018 at 2:41

1 Answer 1

3

After I tested, it would work as follow, you should run these commands on different pipeline but continually:

    List<Command> commandList = new List<Command>();

    Command command1 = new Command("set-adserversettings");
    CommandParameter parameter1 = new CommandParameter("viewentireforest", true);
    command1.Parameters.Add(parameter1);
    commandList.Add(command1);

    Command command2 = new Command("set-userphoto");
    CommandParameter parameter2a = new CommandParameter("identity", tbxName.Text);
    CommandParameter parameter2b = new CommandParameter("picturedata", displayedImage);
    CommandParameter parameter2c = new CommandParameter("domaincontroller", "xx-xx-xx-01.xx.xx.xx.xxx");
    CommandParameter parameter2d = new CommandParameter("confirm", false);
    command2.Parameters.Add(parameter2a);
    command2.Parameters.Add(parameter2b);
    command2.Parameters.Add(parameter2c);
    command2.Parameters.Add(parameter2d);
    commandList.Add(command2);

    Pipeline pipeline;
    Collection<PSObject> exResults = null;

    foreach (Command cmd in commandList)
    {   
        pipeline = runspacee.CreatePipeline();
        pipeline.Commands.Add(cmd);

        exResults = pipeline.Invoke();
    }   
Sign up to request clarification or add additional context in comments.

2 Comments

Really great finding - I was pulling my hair trying to figure out why calling Powershell.Invoke() did ignore set-adserversettings. Iterating through Powershell.Commands, creating a Pipeline for each command finally did make it work. I am not sure why almost nobody else seems to be having this issues...
Maybe that is a bad idea to handle the PowerShell command in .NET code. If there is a more complete library in official to help me handle it, I will use it right away.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.