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

[玩转系统] Runspace02 示例

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

Runspace02 示例


此示例演示如何使用 System.Management.Automation.Powershell 类同步运行 Get-Process 和 Sort-Object cmdlet。 Get-Process cmdlet 返回本地计算机上运行的每个进程的 System.Diagnostics.Process 对象,Sort-Object 根据对象的 System.Diagnostics.Process.Id* 属性对对象进行排序。这些命令的结果通过使用 System.Windows.Forms.Datagridview 控件来显示。

要求

此示例需要 Windows PowerShell 2.0。

示范

该示例演示了以下内容。

  • 创建 System.Management.Automation.Powershell 对象来运行命令。

  • 将命令添加到 System.Management.Automation.Powershell 对象的管道。

  • 同步运行命令。

  • 使用 System.Windows.Forms.Datagridview 控件显示 Windows 窗体应用程序中命令的输出。

例子

此示例在 Windows PowerShell 提供的默认运行空间中同步运行 Get-Process 和 Sort-Object cmdlet。输出使用 System.Windows.Forms.Datagridview 控件显示在表单中。

namespace Microsoft.Samples.PowerShell.Runspaces
{
  using System;
  using System.Collections;
  using System.Collections.ObjectModel;
  using System.Management.Automation;
  using System.Management.Automation.Runspaces;
  using System.Windows.Forms;
  using PowerShell = System.Management.Automation.PowerShell;

  /// <summary>
  /// This class contains the Main entry point for this host application.
  /// </summary>
  internal class Runspace02
  {
    /// <summary>
    /// This method creates the form where the output is displayed.
    /// </summary>
    private static void CreateForm()
    {
      Form form = new Form();
      DataGridView grid = new DataGridView();
      form.Controls.Add(grid);
      grid.Dock = DockStyle.Fill;

      // Create a PowerShell object. Creating this object takes care of
      // building all of the other data structures needed to run the command.
      using (PowerShell powershell = PowerShell.Create())
      {
        powershell.AddCommand("get-process").AddCommand("sort-object").AddArgument("ID");
        if (Runspace.DefaultRunspace == null)
        {
          Runspace.DefaultRunspace = powershell.Runspace;
        }

        Collection<PSObject> results = powershell.Invoke();

        // The generic collection needs to be re-wrapped in an ArrayList
        // for data-binding to work.
        ArrayList objects = new ArrayList();
        objects.AddRange(results);

        // The DataGridView will use the PSObjectTypeDescriptor type
        // to retrieve the properties.
        grid.DataSource = objects;
      }

      form.ShowDialog();
    }

    /// <summary>
    /// This sample uses a PowerShell object to run the Get-Process
    /// and Sort-Object cmdlets synchronously. Windows Forms and
    /// data binding are then used to display the results in a
    /// DataGridView control.
    /// </summary>
    /// <param name="args">The parameter is not used.</param>
    /// <remarks>
    /// This sample demonstrates the following:
    /// 1. Creating a PowerShell object.
    /// 2. Adding commands and arguments to the pipeline of
    ///    the PowerShell object.
    /// 3. Running the commands synchronously.
    /// 4. Using a DataGridView control to display the output
    ///    of the commands in a Windows Forms application.
    /// </remarks>
    private static void Main(string[] args)
    {
      Runspace02.CreateForm();
    }
  }
}

参见

编写 Windows PowerShell 主机应用程序

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

取消回复欢迎 发表评论:

关灯