2025-11-28 00:35:46 +09:00
..
new
2025-11-28 00:35:46 +09:00
new
2025-11-28 00:35:46 +09:00

Windows PowerShell Remoting Disconnect Reconnect Sample

This sample shows how to construct a remote runspace, construct a pipeline to execute a long running script, disconnect the runspace and pipeline, reconstruct the runspace and pipeline state, reconnect the runspace and pipeline, and retrieve command execution state and output data.

The sample includes two scenarios. In the first scenario, a remote runspace object and a pipeline object are created to invoke a long-running script on the remote server. While the script is running, the remote runspace and pipeline objects are disconnected. The same remote runspace and pipeline objects are then reconnected to the server to obtain status information and output data generated by the script.

In the second scenario, a new pipeline object is created using the runspace created in the previous scenario, and the same long-running script is invoked. The remote runspace and pipeline are disconnected while the script runs on the remote host. In this scenario, the remote runspace and pipeline objecs are disposed while in the disconnected state. The remote runspace and pipeline objects are then reconstructed based on information from the remote host session. The remote runspace and pipeline objects are connected to the server to obtain status information and output data from the running script.

Sample Objectives

This sample demonstrates the following:

  1. Creating an instance of the WSManConnectionInfo class.
  2. Using this WSManConnectionInfo instance to establish a remote connection using a remote runspace object.
  3. Creating a pipeline to start a long running script.
  4. Disconnecting the runspace and pipeline and allowing the script to continue running on the server.
  5. Reconnecting the runspace and pipeline to the script running on the server.
  6. Reconstructing a runspace and pipeline for the script running on the server.
  7. Getting runspace and pipeline states and retrieving script output data.
  8. Closing the remote connection.

Windows PowerShell

WSManConnectionInfo

Operating system requirements

Client

Windows 8.1

Server

Windows Server 2012 R2

Build the sample

  1. Start Microsoft Visual Studio and select File > Open > Project/Solution.

  2. Go to the directory named for the sample, and double-click the Visual Studio Solution (.sln) file.

  3. Make sure the project references the System.Management.Automation.dll assembly.

  4. Press F7 or use Build > Build Solution to build the sample.

    The library will be built in the default \bin or \bin\Debug directory.

Run the sample

  1. Verify that Windows PowerShell remoting is enabled. You can run the the following command for additional information about how to enable this feature: help about_remote.
  2. Start the Command Prompt as Administrator.
  3. Navigate to the folder containing the sample executable.
  4. Run the executable.
  5. See the output results and the corresponding code.