简体   繁体   English

将C#管道命令导入PowerShell

[英]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? 我需要一个接一个地运行两个powershell命令,我应该创建两次管道还是有更好的选择? 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. 根据msdn,流水线就像“组装线”一样,因此,如果第一个命令的结果与第二个命令无关,则不需要流水线,可以改用ScriptBlock并使用一个RunspaceInvoke对象。

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...");
}

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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