Click to Rate and Give Feedback
MSDN
MSDN Library
Windows PowerShell
 Creating a Console Application That...

  Switch on low bandwidth view
Creating a Console Application That Adds Commands to a Pipeline

This section describes how to write a console application that adds commands to a pipeline. To do this, the application creates a runspace, creates a pipeline, adds two commands to the pipeline, and then executes the pipeline. The commands added to the pipeline are the Get-Process and Measure-Object cmdlets.

noteNote:
This console application uses the default hosting application (Powershell.exe) and does not define a custom host. You can download the C# source file (Runspace07.cs) for this console application using the Microsoft Windows Software Development Kit for Windows Vista and .NET Framework 3.0 Runtime Components. For download instructions, see How to Install Windows PowerShell and Download the Windows PowerShell SDK.

The downloaded source files are available in the <PowerShell Samples> directory.

Topics in this section are as follows:

Creating and Opening a Runspace

Console applications can create a runspace in several ways, depending on the situation. However, after the runspace is created, it can be opened with a synchronous call to the Open method or an asynchronous call to the OpenAsync method.

This application calls the CreateRunspace method to create the runspace. (Note that the RunspaceInvoke class cannot be used here, because this application must explicitly add commands.) It then calls the Open method to run the runspace. Here is the related code for this application.

C#
Runspace myRunSpace = RunspaceFactory.CreateRunspace();
myRunSpace.Open();

Creating a Pipeline

Console applications must create a Pipeline object to represent the pipeline in which to place the commands. Here is the related code from the sample application.

C#
Pipeline pipeLine = myRunSpace.CreatePipeline();

Adding Commands and Invoking the Pipeline

To add commands to the pipeline, this application uses the Commands property and calls the Add method for each command to be executed by the pipeline.

When all the commands have been added, the pipeline can be invoked by calling the Invoke method. This method processes the pipeline and saves the results. Here is the related code for this application.

C#
using (pipeLine)
{
  // Add the 'get-process' comand to the pipeline (note that in this 
  // case this is just the name of a cmdlet, not a script.)
  pipeLine.Commands.Add("get-process");

  // Then add measure-object to count the number
  // of objects being returned
  pipeLine.Commands.Add("measure-object");


  // Execute the pipeline and save the objects returned.
  result = pipeLine.Invoke();
}

Closing the Pipeline

After the pipeline is processed, the hosting application must close it and allow the garbage collector to clean it up by setting the pipeline variable to null. The following code shows how the sample application closes the runspace.

C#
pipeLine = null;

Displaying the Results of Command Processing

After the pipeline has executed its commands, this application verifies that there are no errors and displays the results to the console. The following code example shows how this application displays the runspace.

C#
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");
}

Console.WriteLine(
                  "Runspace07: 'get-process' returned {0} objects",
                  count.Value);

Closing the Runspace

The application must close the runspace that it has opened. To do this, it calls the Close method. The following code shows how the sample application closes the runspace.

C#
myRunSpace.Close();
myRunSpace = null;

Closing the Pipeline

After the pipeline is processed, the hosting application must close it and allow the garbage collector to clean it up by setting the pipeline variable to null. The following code shows how the sample application closes the runspace.

C#
pipeLine = null;

Code Sample

For complete sample code, see RunSpace07 Code Sample.

Building the Application

For more information about adding this application to the shell, see How to Register Cmdlets, Providers, and Host Applications.

See Also



Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker