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

[玩转系统] Windows PowerShell02 示例

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

Windows PowerShell02 示例


此示例演示如何使用运行空间池的运行空间异步运行命令。该示例生成命令列表,然后运行这些命令,同时 Windows PowerShell 引擎在需要时从池中打开运行空间。

要求

  • 此示例需要 Windows PowerShell 2.0。

示范

该示例演示了以下内容:

  • 创建一个 RunspacePool 对象,该对象具有允许同时打开的最小和最大运行空间数。
  • 创建命令列表。
  • 异步运行命令。
  • 调用 System.Management.Automation.Runspaces.Runspacepool.Getavailablerunspaces* 方法来查看有多少运行空间是空闲的。
  • 使用 System.Management.Automation.Powershell.Endinvoke* 方法捕获命令输出。

例子

此示例演示如何打开运行空间池的运行空间,以及如何在这些运行空间中异步运行命令。

namespace Sample
{
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Management.Automation;
  using System.Management.Automation.Runspaces;
  
  /// <summary>
  /// This class contains the Main entry point for the application.
  /// </summary>
  internal class PowerShell02
  {
    /// <summary>
    /// Runs many commands with the help of a RunspacePool.
    /// </summary>
    /// <param name="args">This parameter is unused.</param>
    private static void Main(string[] args)
    {
      // Creating and opening runspace pool. Use a minimum of 1 runspace and a maximum of 
      // 5 runspaces can be opened at the same time.
      RunspacePool runspacePool = RunspaceFactory.CreateRunspacePool(1, 5);
      runspacePool.Open();
      
      using (runspacePool)
      {
        // Define the commands to be run.
        List<PowerShell> powerShellCommands = new List<PowerShell>();
        
        // The command results.
        List<IAsyncResult> powerShellCommandResults = new List<IAsyncResult>();
        
        // The maximum number of runspaces that can be opened at one time is 
        // 5, but we can queue up many more commands that will use the 
        // runspace pool.
        for (int i = 0; i < 100; i++)
        {
          // Using a PowerShell object, run the commands.
          PowerShell powershell = PowerShell.Create();
                   
          // Instead of setting the Runspace property of powershell,
          // the RunspacePool property is used. That is the only difference
          // between running commands with a runspace and running commands 
          // with a runspace pool.
          powershell.RunspacePool = runspacePool;
          
          // The script to be run outputs a sequence number and the number of available runspaces 
          // in the pool.
          string script = String.Format(
                        "write-output ' Command: {0}, Available Runspaces: {1}'",
                        i,
                        runspacePool.GetAvailableRunspaces());

          // The three lines below look the same running with a runspace or 
          // with a runspace pool.
          powershell.AddScript(script);
          powerShellCommands.Add(powershell);
          powerShellCommandResults.Add(powershell.BeginInvoke());
        }
        
        // Collect the results.
        for (int i = 0; i < 100; i++)
        {
          // EndInvoke will wait for each command to finish, so we will be getting the commands
          // in the same 0 to 99 order that they have been invoked withy BeginInvoke.
          PSDataCollection<PSObject> results = powerShellCommands[i].EndInvoke(powerShellCommandResults[i]);
          
          // Print all the results. One PSObject with a plain string is the expected result.
          PowerShell02.PrintCollection(results);
        }
      }
    }
    
    /// <summary>
    /// Iterates through a collection printing all items.
    /// </summary>
    /// <param name="collection">collection to be printed</param>
    private static void PrintCollection(IList collection)
    {
      foreach (object obj in collection)
      {
        Console.WriteLine("PowerShell Result: {0}", obj);
      }
    }
  }
}

参见

编写 Windows PowerShell 主机应用程序

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

取消回复欢迎 发表评论:

关灯