Runspace02 サンプル

このサンプル では、system.servicemodel クラスを 使用して、 Get Process および Sort-Object コマンドレットを同期的に実行する方法を示します。 Get processコマンドレットは、ローカルコンピューター上で実行されている各プロセスの System.Diagnostics.Process.Id オブジェクトを返し、はそれらのオブジェクトをその Sort-Object *プロパティに基づいて並べ替えます。 これらのコマンドの結果は、Windows を使用して表示されます。 フォームの Datagridviewコントロール。

必要条件

このサンプルには Windows PowerShell 2.0 が必要です。

対象

このサンプルでは、次のことを示します。

  • コマンドを実行するため の、system.servicemodel オブジェクトの作成。

  • System.servicemodel オブジェクトのパイプラインにコマンドを追加しています。

  • コマンドを同期的に実行します。

  • Windows を使用します。Windows フォームアプリケーションのコマンドの出力を表示するためのフォーム Datagridview コントロール。

このサンプルでは、Windows PowerShell によって提供される既定の実行空間で、 Get ProcessおよびSort オブジェクトのコマンドレットを同期的に実行します。 出力は、Windows を使用してフォームに表示されます。 フォームの 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 ホスト アプリケーションを記述する