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

[玩转系统] Runspace10 示例

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

Runspace10 示例


此示例演示如何创建默认初始会话状态、如何将 cmdlet 添加到 System.Management.Automation.Runspaces.Initialsessionstate、如何创建使用初始会话状态的运行空间以及如何使用System.Management.Automation.Powershell 对象。

要求

此示例需要 Windows PowerShell 2.0。

示范

该示例演示了以下内容。

  • 创建 System.Management.Automation.Runspaces.Initialsessionstate 对象。

  • 将 cmdlet(由主机应用程序定义)添加到 System.Management.Automation.Runspaces.Initialsessionstate 对象。

  • 创建使用该对象的 System.Management.Automation.Runspaces.Runspace 对象。

  • 创建使用 System.Management.Automation.Runspaces.Runspace 对象的 System.Management.Automation.Powershell 对象。

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

  • 从命令返回的 System.Management.Automation.PSObject 对象中提取属性。

例子

此示例创建一个运行空间,该运行空间使用 System.Management.Automation.Runspaces.Initialsessionstate 对象来定义打开运行空间时可用的元素。在此示例中,Get-Proc cmdlet(由主机应用程序定义)被添加到初始会话状态,并且该 cmdlet 通过使用 System.Management.Automation.Powershell 对象同步运行。

namespace Microsoft.Samples.PowerShell.Runspaces
{
  using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Diagnostics;
  using System.Management.Automation;
  using System.Management.Automation.Runspaces;
  using PowerShell = System.Management.Automation.PowerShell;

  #region GetProcCommand

  /// <summary>
  /// Class that implements the GetProcCommand.
  /// </summary>
  [Cmdlet(VerbsCommon.Get, "Proc")]
  public class GetProcCommand : Cmdlet
  {
    #region Cmdlet Overrides

    /// <summary>
    /// For each of the requested process names, retrieve and write
    /// the associated processes.
    /// </summary>
    protected override void ProcessRecord()
    {
      // Get the current processes.
      Process[] processes = Process.GetProcesses();

      // Write the processes to the pipeline making them available
      // to the next cmdlet. The second argument (true) tells the
      // system to enumerate the array, and send one process object
      // at a time to the pipeline.
      WriteObject(processes, true);
    }

    #endregion Overrides
  } // End GetProcCommand class.

  #endregion GetProcCommand

  /// <summary>
  /// This class contains the Main entry point for this host application.
  /// </summary>
  internal class Runspace10
  {
    /// <summary>
    /// This sample shows how to create a default initial session state, how to add
    /// add a cmdlet to the InitialSessionState object, and then how to create
    /// a Runspace object.
    /// </summary>
    /// <param name="args">Parameter is not used.</param>
    /// This sample demonstrates:
    /// 1. Creating an InitialSessionState object.
    /// 2. Adding a cmdlet to the InitialSessionState object.
    /// 3. Creating a runspace that uses the InitialSessionState object.
    /// 4. Creating a PowerShell object that uses the Runspace object.
    /// 5. Running the added command synchronously.
    /// 6. Working with PSObject objects to extract properties
    ///    from the objects returned by the pipeline.
    private static void Main(string[] args)
    {
      // Create a default InitialSessionState object. The default
      // InitialSessionState object contains all the elements provided
      // by Windows PowerShell.
      InitialSessionState iss = InitialSessionState.CreateDefault();

      // Add the get-proc cmdlet to the InitialSessionState object.
      SessionStateCmdletEntry ssce = new SessionStateCmdletEntry("get-proc", typeof(GetProcCommand), null);
      iss.Commands.Add(ssce);

      // Create a Runspace object that uses the InitialSessionState object.
      // Notice that no PSHost object is specified, so the default host is used.
      // See the Hosting samples for information on creating your own custom host.
      using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss))
      {
        myRunSpace.Open();

        using (PowerShell powershell = PowerShell.Create())
        {
          powershell.Runspace = myRunSpace;

          // Add the get-proc cmdlet to the pipeline of the PowerShell object.
          powershell.AddCommand("get-proc");

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

          Console.WriteLine("Process              HandleCount");
          Console.WriteLine("--------------------------------");

          // Display the output of the pipeline.
          foreach (PSObject result in results)
          {
             Console.WriteLine(
                               "{0,-20} {1}",
                               result.Members["ProcessName"].Value,
                               result.Members["HandleCount"].Value);
          }
        }

        // Close the runspace to release resources.
        myRunSpace.Close();
      }

      System.Console.WriteLine("Hit any key to exit...");
      System.Console.ReadKey();
    }
  }
}

参见

编写 Windows PowerShell 主机应用程序

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

取消回复欢迎 发表评论:

关灯