简体   繁体   中英

C# Piping commands into PowerShell

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 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 .

Would you be happier with your code if you wrote:

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

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM