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

[玩转系统] 添加和调用命令

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

添加和调用命令


创建运行空间后,您可以将 Windows PowerShell 命令和脚本添加到管道,然后同步或异步调用管道。

创建管道

System.Management.Automation.Powershell 类提供了多种将命令、参数和脚本添加到管道的方法。您可以通过调用 System.Management.Automation.Powershell.Invoke* 方法的重载来同步调用管道,也可以通过调用 System.Management.Automation.Powershell.Begininvoke* 的重载然后调用 System.Management.Automation 来异步调用管道。 .Powershell.Endinvoke* 方法。

AddCommand

  1. 创建 System.Management.Automation.Powershell 对象。

    PowerShell ps = PowerShell.Create();
    
  2. 添加您要执行的命令。

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

    ps.Invoke();
    

如果在调用 System.Management.Automation.Powershell.Invoke* 方法之前多次调用 System.Management.Automation.Powershell.Addcommand* 方法,则第一个命令的结果将通过管道传送到第二个命令,依此类推。如果您不想将上一个命令的结果通过管道传递给命令,请改为通过调用 System.Management.Automation.Powershell.Addstatement* 来添加它。

AddParameter

前面的示例执行不带任何参数的单个命令。您可以使用 System.Management.Automation.Pscommand.Addparameter* 方法向命令添加参数。例如,以下代码获取计算机上运行的所有名为 PowerShell 的进程的列表。

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

您可以通过重复调用 System.Management.Automation.Pscommand.Addparameter* 添加其他参数。

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

您还可以通过调用 System.Management.Automation.Powershell.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

您可以使用 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(File.ReadAllText(@"D:\PSScripts\MyScript.ps1")).Invoke();

还有一个版本的 System.Management.Automation.Powershell.Addscript* 方法采用名为 useLocalScope 的布尔参数。如果此参数设置为 true,则脚本将在本地范围内运行。以下代码将在本地范围内运行脚本。

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

同步调用管道

将元素添加到管道后,您可以调用它。要同步调用管道,请调用 System.Management.Automation.Powershell.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.
}

异步调用管道

您可以通过调用 System.Management.Automation.Powershell.Begininvoke* 的重载来异步调用管道以创建 IAsyncResult 对象,然后调用 System.Management.Automation.Powershell.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.
}

参见

创建初始会话状态

创建受限运行空间

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

取消回复欢迎 发表评论:

关灯