Ejemplo de código Runspace07RunSpace07 Code Sample

Este es el código fuente del ejemplo Runspace07 que se describe en crear una aplicación de consola que agrega comandos a una canalización.Here is the source code for the Runspace07 sample described in Creating a Console Application That Adds Commands to a Pipeline. En esta aplicación de ejemplo se crea un espacio de ejecución, se crea una canalización, se agregan dos comandos a la canalización y, a continuación, se ejecuta la canalización.This sample application creates a runspace, creates a pipeline, adds two commands to the pipeline, and then executes the pipeline. Los comandos que se agregan a la canalización son los Get-Process Measure-Object cmdlets y.The commands added to the pipeline are the Get-Process and Measure-Object cmdlets.


Puede descargar el archivo de código fuente de C# (runspace07.cs) mediante el kit de desarrollo de software de Microsoft Windows para los componentes en tiempo de ejecución de Windows Vista y Microsoft .NET Framework 3,0.You can download the C# source file (runspace07.cs) using the Microsoft Windows Software Development Kit for Windows Vista and Microsoft .NET Framework 3.0 Runtime Components. Para obtener instrucciones de descarga, consulte Cómo instalar Windows PowerShell y descargar el SDK de Windows PowerShell.For download instructions, see How to Install Windows PowerShell and Download the Windows PowerShell SDK. Los archivos de origen descargados están disponibles en el <PowerShell Samples> directorio.The downloaded source files are available in the <PowerShell Samples> directory.

Ejemplo de códigoCode Sample

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

  /// <summary>
  /// This class contains the Main entry point for this host application.
  /// </summary>
  internal class Runspace07
    /// <summary>
    /// This sample shows how to create a runspace and how to run 
    /// commands using a PowerShell object. It builds a pipeline 
    /// that runs the get-process cmdlet, which is piped to the measure-object 
    /// cmdlet to count the number of processes running on the system.
    /// </summary>
    /// <param name="args">Parameter is not used.</param>
    /// <remarks>
    /// This sample demonstrates the following:
    /// 1. Creating a runspace using the RunspaceFactory class.
    /// 2. Creating a PowerShell object
    /// 3. Adding individual cmdlets to the PowerShell object.
    /// 4. Running the cmdlets synchronously.
    /// 5. Working with PSObject objects to extract properties 
    ///    from the objects returned by the cmdlets.
    /// </remarks>
    private static void Main(string[] args)
      Collection<PSObject> result;     // Will hold the result
                                       // of running the cmdlets.

      // Create a runspace. We can not use the RunspaceInvoke class 
      // because we need to get at the underlying runspace to  
      // explicitly add the commands. Notice that no PSHost object is 
      // supplied to the CreateRunspace method so the default host is 
      // used. See the Host samples for more information on creating 
      // your own custom host.
      using (Runspace myRunSpace = RunspaceFactory.CreateRunspace())

        // Create a PowerShell object and specify the runspace. 
        PowerShell powershell = PowerShell.Create();
        powershell.Runspace = myRunSpace;

        // Use the using statement so we dispose of the PowerShell object
        // when we're done.
        using (powershell)
          // Add the get-process cmdlet to the PowerShell object. Notice 
          // we are specify the name of the cmdlet, not a script.

          // Add the measure-object cmdlet to count the number
          // of objects being returned. Commands are always added to the end 
          // of the pipeline.

          // Run the cmdlets synchronously and save the objects returned.
          result = powershell.Invoke();

        // Even after disposing of the pipeLine, we still need to set 
        // the powershell variable to null so that the garbage collector
        // can clean it up.
        powershell = null;

        // Display the results of running the commands (checking that
        // everything is ok first.
        if (result == null || result.Count != 1)
          throw new InvalidOperationException(
                    "pipeline.Invoke() returned the wrong number of objects");

        PSMemberInfo count = result[0].Properties["Count"];
        if (count == null)
          throw new InvalidOperationException(
                    "The object returned doesn't have a 'count' property");

                   "Runspace07: The get-process cmdlet returned {0} objects",

        // Close the runspace to release any resources.
      System.Console.WriteLine("Hit any key to exit...");

Consulte tambiénSee Also

Guía del programador de Windows PowerShellWindows PowerShell Programmer's Guide

Windows PowerShell SDKWindows PowerShell SDK