How to Invoke a Pipeline Asynchronously

This example shows how to invoke a pipeline asynchronously. In this example, the Get-Process cmdlet is run asynchronously and its results are displayed when the operation is completed.

The host application described in the following procedure can be implemented as a console application by using Microsoft Visual Studio.

To invoke a pipeline synchronously

  1. By using Visual Studio, create a new Console Application project.

  2. With the project open, add a reference to the System.Management.Automation.dll assembly.

    noteNote:
    • For Windows XP and Windows Vista operating systems, the assembly is located in the Windows PowerShell installation directory ($PSHome).

    • For Windows 7 operating systems, the assembly is located in the following folder: Windows\assembly\GAC_MSIL\System.Management.Automation.

  3. Add a using statement that references the System.Management.Automation namespace, and remove the default using statements that are not needed. The following statements are the only using statements that are needed for this host application.

    using System;
    using System.Management.Automation;
    
    
  4. Within the Main method of the host application class, call the Create and AddCommand methods to create the pipeline.

    PowerShell ps = PowerShell.Create().AddCommand("Get-Process");
    
    
  5. Create an IAsyncResult object and call the BeginInvoke method to start running the pipeline asynchronously.

    IAsyncResult async = ps.BeginInvoke();
    
    
  6. Call the EndInvoke method, get the results from the IAsyncResult object.

    foreach (PSObject result in ps.EndInvoke(async))
    {
      Console.WriteLine("{0,-20}{1}",
              result.Members["ProcessName"].Value,
              result.Members["Id"].Value);
    } // End foreach.
    
    

For an example of a host application that invokes the commands of the pipeline synchronously, see How to Invoke a Pipeline Synchronously.

Example

This example creates a pipeline that contains the Get-Process cmdlet, and then invokes the pipeline asynchronously on the local computer.

namespace HostPS3
{
    using System;
    using System.Management.Automation;

  class HostPS3
  {
    static void Main(string[] args)
    {
      // Use the PowerShell.Create and Powers.AddCommand 
      // methods to create a command pipeline that includes 
      // Get-Process cmdlet. Do not include spaces immediatly 
      // before or after the cmdlet name as that will cause 
      // the command to fail.
      PowerShell ps = PowerShell.Create().AddCommand("Get-Process");

      // Create an IAsyncResult object and call the
      // BeginInvoke method to start running the 
      // pipeline asynchronously.
      IAsyncResult async = ps.BeginInvoke();
      // Using the PowerShell.EndInvoke method, get the
      // results from the IAsyncResult object.
      foreach (PSObject result in ps.EndInvoke(async))
      {
        Console.WriteLine("{0,-20}{1}",
                result.Members["ProcessName"].Value,
                result.Members["Id"].Value);
      } // End foreach.
      System.Console.WriteLine("Hit any key to exit.");
      System.Console.ReadKey();
    } // End Main.
  } // End HostPS3.
}

See Also



Show:
© 2014 Microsoft