Export (0) Print
Expand All

How to Write a Simple Host Application

This section shows how to write a host application that invokes a command synchronously on the local computer. This example uses the PowerShell class to create a pipeline that contains a single cmdlet and runs that cmdlet in the default runspace provided by Windows PowerShell.

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

How to Write a Simple Host Application

  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;  // Windows PowerShell namespace.
    
    
  4. Call the Create method to create an empty pipeline.

    PowerShell ps = PowerShell.Create();
    
    
  5. Call the AddCommand method to add the Get-Process cmdlet to the pipeline. When specifying the cmdlet name, do not include spaces immediately before or after the cmdlet name as that will cause the command to fail.

    ps.AddCommand("Get-Process");
    
    
  6. Call the Invoke method to run the commands of the pipeline synchronously. This example uses the default runspace and host interface that is provided by Windows PowerShell.

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

For examples of the different ways to create runspaces, see Examples of Creating Runspaces.

For examples of the different ways to create runspace pools, see Examples of Creating Runspace Pools.

For examples of invoking pipelines synchronously or asynchronously, see Examples of Invoking Pipelines..

Example

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

namespace HostSamples
{
  using System;
  using System.Management.Automation;  // Windows PowerShell namespace.

  /// <summary>
  /// This class defines the main entry point for a host application that 
  /// synchronously invokes the following pipeline: 
  /// [Get-Process]
  /// </summary>
  internal class HostPS1
  {
    /// <summary>
    /// The PowerShell object is created and manipulated within the 
    /// Main method.
    /// </summary>
    /// <param name="args">This parameter is not used.</param>
    private static void Main(string[] args)
    {
      // Call the PowerShell.Create() method to create an 
      // empty pipeline.
      PowerShell ps = PowerShell.Create();

      // Call the PowerShell.AddCommand(string) method to add 
      // the Get-Process cmdlet to the pipeline. Do 
      // not include spaces before or after the cmdlet name 
      // because that will cause the command to fail.
      ps.AddCommand("Get-Process");

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

      // Call the PowerShell.Invoke() method to run the 
      // commands of the pipeline.
        foreach (PSObject result in ps.Invoke())
      {
        Console.WriteLine(
                "{0,-24}{1}",
                result.Members["ProcessName"].Value,
                result.Members["Id"].Value);
      } // End foreach.
    } // End Main.
  } // End HostPs1.
}

See Also



Show:
© 2014 Microsoft