共用方式為


新增及叫用命令

建立運行空間之後,您可以將 Windows PowerShellcommands 和腳本新增至管線,然後以同步或非同步方式叫用管線。

建立管線

System.object類別提供數種方法,可將命令、參數和腳本新增至管線。 您可以藉由呼叫 Begininvoke * 的多 載或以非同步方式叫用管線,方法是呼叫 * 的多載,然後呼叫 Endinvoke * 方法的多載,以同步方式叫用管線。

AddCommand

  1. Create a System.Management.Automation.Powershell object.

    PowerShell ps = PowerShell.Create();
    
  2. 新增您想要執行的命令。

    ps.AddCommand("Get-Process");
    
  3. 叫用命令。

    ps.Invoke();
    

如果您在呼叫 Addcommand * 方法之前 多次呼叫 * 方法,則第一個命令的結果會以管道傳送至第二個命令,依此類推,依此類推.. (秒)。 如果您不想要使用管線將上一個命令的結果傳送至命令,請改為呼叫 Addstatement * 來新增。

AddParameter

先前的範例會執行不含任何參數的單一命令。 您可以使用 Pscommand. Addparameter * 方法將參數加入至命令,例如,下列程式碼會取得在電腦上執行的所有處理常式的清單 PowerShell

PowerShell.Create().AddCommand("Get-Process")
                   .AddParameter("Name", "PowerShell")
                   .Invoke();

您可以重複呼叫 Pscommand. Addparameter * 來新增其他參數。

PowerShell.Create().AddCommand("Get-Command")
                   .AddParameter("Name", "Get-VM")
                   .AddParameter("Module", "Hyper-V")
                   .Invoke();

您也可以藉由呼叫 Addparameters * 方法,新增參數名稱和值的字典。

IDictionary parameters = new Dictionary<String, String>();
parameters.Add("Name", "Get-VM");

parameters.Add("Module", "Hyper-V");
PowerShell.Create().AddCommand("Get-Command")
   .AddParameters(parameters)
      .Invoke()

AddStatement

您可以使用 Addstatement * 方法來模擬批次處理,這會在管線的結尾加入額外的語句。下列程式碼會取得名稱為的執行中進程清單 PowerShell ,然後取得執行中服務的清單。

PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-Process").AddParameter("Name", "PowerShell");
ps.AddStatement().AddCommand("Get-Service");
ps.Invoke();

AddScript

您可以藉由呼叫 Addscript * 方法來執行現有的腳本。 下列範例會將腳本新增至管線,並加以執行。 此範例假設名為的資料夾中已經有一個名為的腳本 MyScript.ps1 D:\PSScripts

PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"D:\PSScripts\MyScript.ps1")).Invoke();

另外還有一個 Addscript * 方法的版本,它會採用名為的布林參數 useLocalScope 。 如果此參數設定為 true ,則腳本會在本機範圍中執行。 下列程式碼會在本機範圍中執行腳本。

PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"D:\PSScripts\MyScript.ps1"), true).Invoke();

以同步方式叫用管線

將專案新增至管線之後,您會叫用它。 若要以同步方式叫用管線,您可以呼叫 的多 載。 下列範例示範如何同步叫用管線。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;

namespace HostPS1e
{
  class HostPS1e
  {
    static void Main(string[] args)
    {
      // Using the PowerShell.Create and AddCommand
      // methods, create a command pipeline.
      PowerShell ps = PowerShell.Create().AddCommand ("Sort-Object");

      // Using the PowerShell.Invoke method, run the command
      // pipeline using the supplied input.
      foreach (PSObject result in ps.Invoke(new int[] { 3, 1, 6, 2, 5, 4 }))
      {
          Console.WriteLine("{0}", result);
      } // End foreach.
    } // End Main.
  } // End HostPS1e.
}

以非同步方式叫用管線

您可以呼叫 Begininvoke * 的多載,以非同步方式叫用管線以建立 IAsyncResult 物件,然後再呼叫 Endinvoke * 方法,藉此叫用管線。

下列範例顯示如何以非同步方式叫用管線。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;

namespace HostPS3
{
  class HostPS3
  {
    static void Main(string[] args)
    {
      // Use the PowerShell.Create and PowerShell.AddCommand
      // methods to create a command pipeline that includes
      // Get-Process cmdlet. Do not include spaces immediately
      // before or after the cmdlet name as that will cause
      // the command to fail.
      PowerShell ps = PowerShell.Create().AddCommand("Get-Process");

      // Create an IAsyncResult object and call the
      // BeginInvoke method to start running the
      // command pipeline asynchronously.
      IAsyncResult asyncpl = ps.BeginInvoke();

      // Using the PowerShell.Invoke method, run the command
      // pipeline using the default runspace.
      foreach (PSObject result in ps.EndInvoke(asyncpl))
      {
        Console.WriteLine("{0,-20}{1}",
                result.Members["ProcessName"].Value,
                result.Members["Id"].Value);
      } // End foreach.
      System.Console.WriteLine("Hit any key to exit.");
      System.Console.ReadKey();
    } // End Main.
  } // End HostPS3.
}

另請參閱

建立 InitialSessionState

建立受限 Runspace