Skip to content

Commit 9a9ea99

Browse files
davidsmatlaksdwheeler
authored andcommitted
issue 4788 (#4807)
1 parent af8a928 commit 9a9ea99

File tree

1 file changed

+109
-96
lines changed

1 file changed

+109
-96
lines changed
Lines changed: 109 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,109 @@
1-
---
2-
title: "Creating remote runspaces | Microsoft Docs"
3-
ms.custom: ""
4-
ms.date: "09/12/2016"
5-
ms.reviewer: ""
6-
ms.suite: ""
7-
ms.tgt_pltfrm: ""
8-
ms.topic: "article"
9-
ms.assetid: 057a666f-731b-423d-9d80-7be6b1836244
10-
caps.latest.revision: 5
11-
---
12-
# Creating remote runspaces
13-
14-
Windows PowerShell commands that take a `ComputerName` parameter can be run on any computer that runs Windows PowerShell. To run commands that do not take a `ComputerName` parameter, you can use WS-Management to configure a runspace that connects to a specified computer, and run commands on that computer.
15-
16-
## Using a WSManConnection to create a remote runspace
17-
18-
To create a runspace that connects to a remote computer, you create a [System.Management.Automation.Runspaces.Wsmanconnectioninfo](/dotnet/api/System.Management.Automation.Runspaces.WSManConnectionInfo) object. You specify the target endpoint for the connection by setting the [System.Management.Automation.Runspaces.Wsmanconnectioninfo.Connectionuri*](/dotnet/api/System.Management.Automation.Runspaces.WSManConnectionInfo.ConnectionUri) property of the object. You then create a runspace by calling the [System.Management.Automation.Runspaces.Runspacefactory.Createrunspace*](/dotnet/api/System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace) method, specifying the [System.Management.Automation.Runspaces.Wsmanconnectioninfo](/dotnet/api/System.Management.Automation.Runspaces.WSManConnectionInfo) object as the `connectionInfo` parameter.
19-
20-
The following example shows how to create a runspace that connects to a remote computer. In the example, `RemoteComputerUri` is used as a placeholder for the actual URI of a remote computer.
21-
22-
```csharp
23-
namespace Samples
24-
{
25-
using System;
26-
using System.Collections.ObjectModel;
27-
using System.Management.Automation; // Windows PowerShell namespace.
28-
using System.Management.Automation.Runspaces; // Windows PowerShell namespace.
29-
30-
/// <summary>
31-
/// This class contains the Main entry point for this host application.
32-
/// </summary>
33-
internal class RemoteRunspace02
34-
{
35-
/// <summary>
36-
/// This sample shows how to create a remote runspace that
37-
/// runs commands on the local computer.
38-
/// </summary>
39-
/// <param name="args">Parameter not used.</param>
40-
private static void Main(string[] args)
41-
{
42-
// Create a WSManConnectionInfo object using the default constructor
43-
// to connect to the "localHost". The WSManConnectionInfo object can
44-
// also be used to specify connections to remote computers.
45-
WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
46-
47-
// Set the OperationTimeout property and OpenTimeout properties.
48-
// The OperationTimeout property is used to tell Windows PowerShell
49-
// how long to wait (in milliseconds) before timing out for an
50-
// operation. The OpenTimeout property is used to tell Windows
51-
// PowerShell how long to wait (in milliseconds) before timing out
52-
// while establishing a remote connection.
53-
connectionInfo.OperationTimeout = 4 * 60 * 1000; // 4 minutes.
54-
connectionInfo.OpenTimeout = 1 * 60 * 1000; // 1 minute.
55-
56-
// Create a remote runspace using the connection information.
57-
//using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace())
58-
using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
59-
{
60-
// Establish the connection by calling the Open() method to open the runspace.
61-
// The OpenTimeout value set previously will be applied while establishing
62-
// the connection. Establishing a remote connection involves sending and
63-
// receiving some data, so the OperationTimeout will also play a role in this process.
64-
remoteRunspace.Open();
65-
66-
// Create a PowerShell object to run commands in the remote runspace.
67-
using (PowerShell powershell = PowerShell.Create())
68-
{
69-
powershell.Runspace = remoteRunspace;
70-
powershell.AddCommand("get-process");
71-
powershell.Invoke();
72-
73-
Collection<PSObject> results = powershell.Invoke();
74-
75-
Console.WriteLine("Process HandleCount");
76-
Console.WriteLine("--------------------------------");
77-
78-
// Display the results.
79-
foreach (PSObject result in results)
80-
{
81-
Console.WriteLine(
82-
"{0,-20} {1}",
83-
result.Members["ProcessName"].Value,
84-
result.Members["HandleCount"].Value);
85-
}
86-
}
87-
88-
// Close the connection. Call the Close() method to close the remote
89-
// runspace. The Dispose() method (called by using primitive) will call
90-
// the Close() method if it is not already called.
91-
remoteRunspace.Close();
92-
}
93-
}
94-
}
95-
}
96-
```
1+
---
2+
title: "Creating remote runspaces | Microsoft Docs"
3+
ms.custom: ""
4+
ms.date: "09/12/2016"
5+
ms.reviewer: ""
6+
ms.suite: ""
7+
ms.tgt_pltfrm: ""
8+
ms.topic: "article"
9+
ms.assetid: 057a666f-731b-423d-9d80-7be6b1836244
10+
caps.latest.revision: 5
11+
---
12+
13+
# Creating remote runspaces
14+
15+
PowerShell commands that take a **ComputerName** parameter can be run on any computer that runs
16+
PowerShell. To run commands that don't take a **ComputerName** parameter, you can use WS-Management
17+
to configure a runspace that connects to a specified computer, and run commands on that computer.
18+
19+
## Using a WSManConnection to create a remote runspace
20+
21+
To create a runspace that connects to a remote computer, you create a
22+
[System.Management.Automation.Runspaces.WSManConnectionInfo](/dotnet/api/System.Management.Automation.Runspaces.WSManConnectionInfo)
23+
object. You specify the target endpoint for the connection by setting the
24+
[System.Management.Automation.Runspaces.WSManConnectionInfo.ConnectionUri](/dotnet/api/System.Management.Automation.Runspaces.WSManConnectionInfo.ConnectionUri)
25+
property of the object. You then create a runspace by calling the
26+
[System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace](/dotnet/api/System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace)
27+
method, specifying the
28+
[System.Management.Automation.Runspaces.WSManConnectionInfo](/dotnet/api/System.Management.Automation.Runspaces.WSManConnectionInfo)
29+
object as the `connectionInfo` parameter.
30+
31+
The following example shows how to create a runspace that connects to a remote computer. In the
32+
example, `RemoteComputerUri` is used as a placeholder for the actual URI of a remote computer.
33+
34+
```csharp
35+
namespace Samples
36+
{
37+
using System;
38+
using System.Collections.ObjectModel;
39+
using System.Management.Automation; // PowerShell namespace.
40+
using System.Management.Automation.Runspaces; // PowerShell namespace.
41+
42+
/// <summary>
43+
/// This class contains the Main entry point for this host application.
44+
/// </summary>
45+
internal class RemoteRunspace02
46+
{
47+
/// <summary>
48+
/// This sample shows how to create a remote runspace that
49+
/// runs commands on the local computer.
50+
/// </summary>
51+
/// <param name="args">Parameter not used.</param>
52+
private static void Main(string[] args)
53+
{
54+
// Create a WSManConnectionInfo object using the default constructor
55+
// to connect to the "localHost". The WSManConnectionInfo object can
56+
// also be used to specify connections to remote computers.
57+
Uri RemoteComputerUri = new uri("http://Server01:5985/WSMAN");
58+
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(RemoteComputerUri);
59+
60+
// Set the OperationTimeout property and OpenTimeout properties.
61+
// The OperationTimeout property is used to tell PowerShell
62+
// how long to wait (in milliseconds) before timing out for an
63+
// operation. The OpenTimeout property is used to tell Windows
64+
// PowerShell how long to wait (in milliseconds) before timing out
65+
// while establishing a remote connection.
66+
connectionInfo.OperationTimeout = 4 * 60 * 1000; // 4 minutes.
67+
connectionInfo.OpenTimeout = 1 * 60 * 1000; // 1 minute.
68+
69+
// Create a remote runspace using the connection information.
70+
//using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace())
71+
using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
72+
{
73+
// Establish the connection by calling the Open() method to open the runspace.
74+
// The OpenTimeout value set previously will be applied while establishing
75+
// the connection. Establishing a remote connection involves sending and
76+
// receiving some data, so the OperationTimeout will also play a role in this process.
77+
remoteRunspace.Open();
78+
79+
// Create a PowerShell object to run commands in the remote runspace.
80+
using (PowerShell powershell = PowerShell.Create())
81+
{
82+
powershell.Runspace = remoteRunspace;
83+
powershell.AddCommand("get-process");
84+
powershell.Invoke();
85+
86+
Collection<PSObject> results = powershell.Invoke();
87+
88+
Console.WriteLine("Process HandleCount");
89+
Console.WriteLine("--------------------------------");
90+
91+
// Display the results.
92+
foreach (PSObject result in results)
93+
{
94+
Console.WriteLine(
95+
"{0,-20} {1}",
96+
result.Members["ProcessName"].Value,
97+
result.Members["HandleCount"].Value);
98+
}
99+
}
100+
101+
// Close the connection. Call the Close() method to close the remote
102+
// runspace. The Dispose() method (called by using primitive) will call
103+
// the Close() method if it is not already called.
104+
remoteRunspace.Close();
105+
}
106+
}
107+
}
108+
}
109+
```

0 commit comments

Comments
 (0)