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.
Topics in this section
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.
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.
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.
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.
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.
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.
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.
Code Sample
See Also