当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Windows PowerShell 主机快速入门

作者:精品下载站 日期:2024-12-14 02:46:32 浏览:12 分类:玩电脑

Windows PowerShell 主机快速入门


要在应用程序中托管 Windows PowerShell,请使用 System.Management.Automation.PowerShell 类。此类提供了创建命令管道然后在运行空间中执行这些命令的方法。创建主机应用程序的最简单方法是使用默认运行空间。默认运行空间包含所有核心 Windows PowerShell 命令。如果您希望应用程序仅公开 Windows PowerShell 命令的子集,则必须创建自定义运行空间。

使用默认运行空间

首先,我们将使用默认运行空间,并使用 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();
    

如果在调用 System.Management.Automation.PowerShell.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 方法添加参数名称和值的字典。

IDictionary parameters = new Dictionary<String, String>();
parameters.Add("Path", @"c:\Windows");
parameters.Add("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 方法来运行现有脚本。以下示例将脚本添加到管道并运行它。此示例假设名为 D:\PSScripts 的文件夹中已有一个名为 MyScript.ps1 的脚本。

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();

创建自定义运行空间

虽然前面示例中使用的默认运行空间会加载所有核心 Windows PowerShell 命令,但您可以创建仅加载所有命令的指定子集的自定义运行空间。您可能希望这样做是为了提高性能(加载大量命令会影响性能),或者限制用户执行操作的能力。仅公开有限数量命令的运行空间称为受限运行空间。要创建受限运行空间,请使用 System.Management.Automation.Runspaces.Runspace 和 System.Management.Automation.Runspaces.InitialSessionState 类。

创建一个 InitialSessionState 对象

要创建自定义运行空间,必须首先创建 System.Management.Automation.Runspaces.InitialSessionState 对象。在以下示例中,我们在创建默认的 InitialSessionState 对象后使用 System.Management.Automation.Runspaces.RunspaceFactory 创建运行空间。

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();

限制运行空间

在前面的示例中,我们创建了一个默认的 System.Management.Automation.Runspaces.InitialSessionState 对象,该对象加载所有内置核心 Windows PowerShell。我们还可以调用 System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2 方法来创建一个 InitialSessionState 对象,该对象将仅加载 Microsoft.PowerShell.Core 管理单元中的命令。若要创建更受约束的运行空间,必须通过调用 System.Management.Automation.Runspaces.InitialSessionState.Create 方法创建一个空的 InitialSessionState 对象,然后向该 InitialSessionState 添加命令。

使用仅加载您指定的命令的运行空间可显着提高性能。

您可以使用 System.Management.Automation.Runspaces.SessionStateCmdletEntry 类的方法来定义初始会话状态的 cmdlet。以下示例创建一个空的初始会话状态,然后定义 Get-CommandImport-Module 命令并将其添加到初始会话状态。然后,我们创建一个受初始会话状态约束的运行空间,并在该运行空间中执行命令。

创建初始会话状态。

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 rs = RunspaceFactory.CreateRunspace(iss);
rs.Open();

执行命令并显示结果。

PowerShell ps = PowerShell.Create();
ps.Runspace = rs;
ps.AddCommand("Get-Command");
Collection<CommandInfo> result = ps.Invoke<CommandInfo>();
foreach (var entry in result)
{
    Console.WriteLine(entry.Name);
}

关闭运行空间。

rs.Close();

运行时,该代码的输出将如下所示。

Get-Command
Import-Module

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯