2009-07-05 93 views
1

我需要一個接一個地運行兩個powershell命令,我應該創建一個管道兩次還是有更好的選擇?由於C#將命令傳入PowerShell

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

回答

0

你與你的代碼更快樂,如果你寫道:

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

+0

這有什麼區別?謝謝 – 2009-07-05 23:10:20

1

根據msdn,管道就像是一個「裝配線」,所以如果第一個命令的結果與第二個命令無關,那麼不需要管道,您可以使用ScriptBlock代替,並用RunspaceInvoke對象調用它。

查看示例here