当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] RemoteRunspace01 示例

作者:精品下载站 日期:2024-12-14 02:44:46 浏览:15 分类:玩电脑

RemoteRunspace01 示例


此示例演示如何创建用于建立远程连接的远程运行空间。

要求

此示例需要 Windows PowerShell 2.0。

示范

  • 创建 System.Management.Automation.Runspaces.Wsmanconnectioninfo 对象。

  • 设置 System.Management.Automation.Runspaces.Wsmanconnectioninfo 对象的 System.Management.Automation.Runspaces.Runspaceconnectioninfo.Operationtimeout* 和 System.Management.Automation.Runspaces.Runspaceconnectioninfo.Opentimeout* 属性。

  • 创建使用 System.Management.Automation.Runspaces.Wsmanconnectioninfo 对象建立远程连接的远程运行空间。

  • 关闭远程运行空间以释放远程连接。

例子

此示例定义远程连接,然后使用该连接信息建立远程连接。

namespace Microsoft.Samples.PowerShell.Runspaces
{
  using System;
  using System.Management.Automation;             // Windows PowerShell namespace.
  using System.Management.Automation.Runspaces;   // Windows PowerShell namespace.

  /// <summary>
  /// This class contains the Main entry point for the application.
  /// </summary>
  internal class RemoteRunspace01
  {
    /// <summary>
    /// This sample shows how to use a WSManConnectionInfo object to set
    /// various timeouts and how to establish a remote connection.
    /// </summary>
    /// <param name="args">This parameter is not used.</param>
    public static void Main(string[] args)
    {
      // Create a WSManConnectionInfo object using the default constructor
      // to connect to the "localHost". The WSManConnectionInfo object can
      // also specify connections to remote computers.
      WSManConnectionInfo connectionInfo = new WSManConnectionInfo();

      // Set the OperationTimeout property. The OperationTimeout is used to tell
      // Windows PowerShell how long to wait (in milliseconds) before timing out
      // for any operation. This includes sending input data to the remote computer,
      // receiving output data from the remote computer, and more. The user can
      // change this timeout depending on whether the connection is to a computer
      // in the data center or across a slow WAN.
      connectionInfo.OperationTimeout = 4 * 60 * 1000; // 4 minutes.

      // Set the OpenTimeout property. OpenTimeout is used to tell Windows PowerShell
      // how long to wait (in milliseconds) before timing out while establishing a
      // remote connection. The user can change this timeout depending on whether the
      // connection is to a computer in the data center or across a slow WAN.
      connectionInfo.OpenTimeout = 1 * 60 * 1000; // 1 minute.

      // Create a remote runspace using the connection information.
      using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
      {
        // Establish the connection by calling the Open() method to open the runspace.
        // The OpenTimeout value set previously will be applied while establishing
        // the connection. Establishing a remote connection involves sending and
        // receiving some data, so the OperationTimeout will also play a role in this process.
        remoteRunspace.Open();

        // Add the code to run commands in the remote runspace here. The
        // OperationTimeout value set previously will play a role here because
        // running commands involves sending and receiving data.

        // Close the connection. Call the Close() method to close the remote
        // runspace. The Dispose() method (called by using primitive) will call
        // the Close() method if it is not already called.
        remoteRunspace.Close();
      }
    }
  }
}

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯