共用方式為


Windows PowerShell 主機快速入門

若要在應用程式中裝載 Windows PowerShell,請使用 System.Management.Automation.PowerShell 類別。 這個類別提供建立命令管線的方法,然後在 Runspace 中執行這些命令。 建立主應用程式最簡單的方式是使用預設 Runspace。 默認 Runspace 包含所有核心 Windows PowerShell 命令。 如果您想要讓應用程式只公開 Windows PowerShell 命令的子集,您必須建立自定義 Runspace。

備註

若要執行下列範例,您必須 Microsoft.PowerShell.SDK 安裝 NuGet 套件。

使用預設 Runspace

首先,我們將使用預設 Runspace,並使用 System.Management.Automation.PowerShell 類別的方法,將命令、參數、語句和腳本新增至管線。

AddCommand

您可以使用 System.Management.Automation.PowerShell。AddCommand 方法,將命令新增至管線。 例如,假設您想要取得計算機上執行中的進程清單。 執行此命令的方式如下。

  1. 建立 System.Management.Automation.PowerShell 物件。

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

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

    ps.Invoke();
    

如果您在呼叫 AddCommand 方法之前多次呼叫 方法,則第一個命令的結果會以管線傳送至第二個,依序傳送。 如果您不想使用管線將上一個命令的結果傳送至命令,請呼叫 System.Management.Automation.PowerShell 來新增它。請改為AddStatement

AddParameter

上述範例會執行不含任何參數的單一命令。 您可以使用 System.Management.Automation.PSCommand,將參數新增至命令。AddParameter 方法。 例如,下列程式代碼會取得計算機上執行 powershell 命名的所有進程清單。

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

您可以重複呼叫 AddParameter 方法來新增其他參數。

PowerShell.Create().AddCommand("Get-ChildItem")
                   .AddParameter("Path", @"C:\Windows")
                   .AddParameter("Filter", "*.exe")
                   .Invoke();

您也可以呼叫 System.Management.Automation.PowerShell.AddParameters 方法來新增參數名稱和值的字典。

var parameters = new Dictionary<string, string>
{
    { "Path", @"C:\Windows" },
    { "Filter", "*.exe" }
};

PowerShell.Create().AddCommand("Get-Process")
                   .AddParameters(parameters)
                   .Invoke()

AddStatement

您可以使用 System.Management.Automation.PowerShell 來模擬批處理。AddStatement 方法,它會將額外的語句新增至管線結尾。 下列程式代碼會取得名稱為 powershell的執行中進程清單,然後取得執行中的服務清單。

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

AddScript

您可以呼叫 System.Management.Automation.PowerShell 來執行現有的腳本。AddScript 方法。 下列範例會將腳本新增至管線並加以執行。 此範例假設在名為 MyScript.ps1的資料夾中已經有名為 D:\PSScripts 的腳本。

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

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

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

建立自定義 Runspace

雖然先前範例中使用的預設 Runspace 會載入所有核心 Windows PowerShell 命令,但您可以建立自定義 Runspace,只載入所有命令的指定子集。 您可能想要這樣做以改善效能(載入較多的命令是效能命中),或限制使用者執行作業的功能。 只公開有限數目命令的 Runspace 稱為受限制的 Runspace。 若要建立受限制的 Runspace,您可以使用 System.Management.Automation.Runspaces.RunspaceSystem.Management.Automation.Runspaces.InitialSessionState 類別。

建立 InitialSessionState 物件

若要建立自定義 Runspace,您必須先建立 System.Management.Automation.Runspaces.InitialSessionState 物件。 在下列範例中,我們使用 System.Management.Automation.Runspaces.RunspaceFactory 建立預設 InitialSessionState 對象之後建立 Runspace。

InitialSessionState iss = InitialSessionState.CreateDefault();

Runspace rs = RunspaceFactory.CreateRunspace(iss);
rs.Open();

PowerShell ps = PowerShell.Create();
ps.Runspace = rs;
ps.AddCommand("Get-Command");
ps.Invoke();

rs.Close();

限制 Runspace

在上一個範例中,我們已建立預設 System.Management.Automation.Runspaces.InitialSessionState 物件,以載入所有內建核心 Windows PowerShell。 我們也可以呼叫 System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2 方法來建立 InitialSessionState 物件,該物件只會載入 Microsoft.PowerShell.Core 嵌入式管理單元中的命令。 若要建立更受限的 Runspace,您必須呼叫 System.Management.Automation.Runspaces.InitialSessionState.Create 方法,然後將命令新增至 InitialSessionState,以建立空的 InitialSessionState 物件。

使用僅載入您指定之命令的 Runspace 可提供大幅改善的效能。

您可以使用 System.Management.Automation.Runspaces.SessionStateCmdletEntry 類別的方法,定義初始會話狀態的 Cmdlet。 下列範例會建立空的初始工作階段狀態,然後定義並將 Get-CommandImport-Module 命令新增至初始工作階段狀態。 然後,我們會建立受該初始會話狀態限制的 Runspace,並在該 Runspace 中執行命令。

建立初始工作階段狀態。

InitialSessionState iss = InitialSessionState.Create();

定義命令並新增至初始工作階段狀態。

SessionStateCmdletEntry getCommand = new SessionStateCmdletEntry(
    "Get-Command", typeof(Microsoft.PowerShell.Commands.GetCommandCommand), "");
SessionStateCmdletEntry importModule = new SessionStateCmdletEntry(
    "Import-Module", typeof(Microsoft.PowerShell.Commands.ImportModuleCommand), "");

iss.Commands.Add(getCommand);
iss.Commands.Add(importModule);

建立並開啟 Runspace。

Runspace rs = RunspaceFactory.CreateRunspace(iss);
rs.Open();

執行命令並顯示結果。

PowerShell ps = PowerShell.Create();
ps.Runspace = rs;
ps.AddCommand("Get-Command");

Collection<CommandInfo> result = ps.Invoke<CommandInfo>();

foreach (CommandInfo entry in result)
{
    Console.WriteLine(entry.Name);
}

關閉 Runspace。

rs.Close();

執行時,此程式代碼的輸出如下所示。

Get-Command
Import-Module