Runspace Samples

This section includes sample code that shows how to use different types of runspaces to run commands synchronously and asynchronously. You can use Microsoft Visual Studio to create a console application and then copy the code from the topics in this section into your host application.

In This Section

noteNote:
For samples of host applications that create custom host interfaces, see Custom Host Samples.

Runspace01 Sample
This sample shows how to use the PowerShell class to run the Get-Process cmdlet synchronously and display its output in a console window.

Runspace02 Sample
This sample shows how to use the PowerShell class to run the Get-Process and Sort-Object cmdlets synchronously. The results of these commands is displayed by using a DataGridView control.

Runspace03 Sample
This sample shows how to use the PowerShell class to run a script synchronously, and how to handle non-terminating errors. The script receives a list of process names and then retrieves those processes. The results of the script, including any non-terminating errors that were generated when running the script, are displayed in a console window.

Runspace04 Sample
This sample shows how to use the PowerShell class to run commands, and how to catch terminating errors that are thrown when running the commands. Two commands are run, and the last command is passed a parameter argument that is not valid. As a result no objects are returned and a terminating error is thrown.

Runspace05 Sample
This sample shows how to add a snap-in to an InitialSessionState object so that the cmdlet of the snap-in is available when the runspace is opened. The snap-in provides a Get-Proc cmdlet (defined by the GetProcessSample01 Sample) that is run synchronously using a PowerShell object.

Runspace06 Sample
This sample shows how to add a module to an InitialSessionState object so that the module is loaded when the runspace is opened. The module provides a Get-Proc cmdlet (defined by the GetProcessSample02 Sample) that is run synchronously using a PowerShell object.

Runspace07 Sample
This sample shows how to create a runspace, and then use that runspace to run two cmdlets synchronously by using a PowerShell object.

Runspace08 Sample
This sample shows how to add commands and arguments to the pipeline of a PowerShell object and how to run the commands synchronously.

Runspace09 Sample
This sample shows how to add a script to the pipeline of a PowerShell object and how to run the script asynchronously. Events are used to handle the output of the script.

Runspace10 Sample
This sample shows how to create a default initial session state, how to add a cmdlet to the InitialSessionState, how to create a runspace that uses the initial session state, and how to run the command by using a PowerShell object.

Runspace11 Sample
This shows how to use the ProxyCommand class to create a proxy command that calls an existing cmdlet, but restricts the set of available parameters. The proxy command is then added to an initial session state that is used to create a constrained runspace. This means that the user can access the functionality of the cmdlet only through the proxy command.

See Also



Show:
© 2014 Microsoft