RunspacePool Class (System.Management.Automation.Runspaces)

Switch View :
ScriptFree
RunspacePool Class
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Maintains a group of runspaces that have the same characteristics, and can be opened and closed on an as-needed basis. Runspace pools can be used to execute multiple commands concurrently, with each command invoked in a different runspace. This class is introduced in Windows PowerShell 2.0.


Namespace: System.Management.Automation.Runspaces
Assembly: System.Management.Automation (in System.Management.Automation)
Usage

Visual Basic
Dim instance As RunspacePool

Syntax

Visual Basic
Public NotInheritable Class RunspacePool
	Implements IDisposable
C#
public sealed class RunspacePool : IDisposable
C++
public ref class RunspacePool sealed : IDisposable
J#
public final class RunspacePool implements IDisposable
JScript
public final class RunspacePool implements IDisposable
Example

This example shows how to create a runspace pool object that has a single runspace, open the runspace, use a PowerShell object to run commands in the runspace, how to close the runspace, and then how to dispose the runspace pool object to free resources.

C#
using (RunspacePool rsp = RunspaceFactory.CreateRunspacePool())
{
  rsp.Open();
  
  // Create a PowerShell object to run the following command.
  //  get-process wmi*
  PowerShell gpc = PowerShell.Create();        
  // Specify the runspace to use and add commands.
  gpc.RunspacePool = rsp;
  gpc.AddCommand("Get-Process").AddArgument("wmi*");

  // Invoke the command asynchronously. 
  IAsyncResult gpcAsyncResult = gpc.BeginInvoke();
  // Get the results of running the command. 
  PSDataCollection<PSObject> gpcOutput = gpc.EndInvoke(gpcAsyncResult);

  // Process the output.
  Console.WriteLine("The output from running the command: get-process wmi*");
  for (int i= 0; i < gpcOutput.Count; i++)
  {
   Console.WriteLine(
                     "Process Name: {0} Process Id: {1}",
                     gpcOutput[i].Properties["ProcessName"].Value,
                     gpcOutput[i].Properties["Id"].Value);
  }

Remarks

A runspace is an environment where commands can be run. Each runspace has a global session-state, plus session-states for any modules that are loaded in the runspace.

noteNote:
From a user’s perspective, a runspace is the session that they are currently using or a PSSession that they have created on a computer.

To create a runspace pool, call the CreateRunspacePool method of the RunspaceFactory class. You can create a pool of runspaces on the local computer or a remote computer.


Inheritance Hierarchy

System.Object
  System.Management.Automation.Runspaces.RunspacePool
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

 

Target Platforms

Windows 98, Windows 2000, Windows 2000 Server, Windows CE, Windows Server 2008, Windows 98 Second Edition, Pocket PC, Smart Phone, Windows Server 2003, Windows XP Professional, Windows Vista, Windows Server 2003 R2, Windows XP, Windows 7, Windows 2008 R2, Windows Developer Preview, Windows Server Developer Preview
Change History

See Also

Send comments about this topic to Microsoft.