tags:

views:

259

answers:

2

Hi all,

I need to run two powershell commands one after the other, should I create a pipeline twice or is there a better option? Thanks

Runspace myRunSpace = RunspaceFactory.CreateRunspace(rc);
            myRunSpace.Open();

            Pipeline pipeLine = myRunSpace.CreatePipeline();

            using (pipeLine)
            {
                Command myCommand = new Command("Get-Command...");
            }

            Pipeline pipeLine2 = myRunSpace.CreatePipeline();

            using (pipeLine2)
            {
                Command myCommand = new Command("Get-Command...");
            }
A: 

Would you be happier with your code if you wrote:

using (Pipeline pipeLine = myRunSpace.CreatePipeline())
{
    Command myCommand = new Command("Get-Command...");
}

?

Jay Bazuzi
Would this make any difference? Thanks
Jade M
Not in behavior, but it is slightly cleaner code.
Jay Bazuzi
+1  A: 

A pipeline is, according to msdn, like an "assembly line", so if the results of the first command have nothing to do with the second command, you do not need a Pipeline, you can use a ScriptBlock instead, and invoke it with a RunspaceInvoke object.

See an example here.

kek444