Bbb
Bbb

Reputation: 649

Run multiple powershell commands

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();

Upvotes: 2

Views: 2421

Answers (1)

John Jang
John Jang

Reputation: 2965

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();
    }   

Upvotes: 3

Related Questions