【问题标题】:Check remote PowerShell session status in C#在 C# 中检查远程 PowerShell 会话状态
【发布时间】:2014-02-08 05:45:05
【问题描述】:

我根据以下代码创建 PowerShell 远程处理会话:

AuthenticationMechanism auth = AuthenticationMechanism.Negotiate;

WSManConnectionInfo ci = new WSManConnectionInfo(
    false,
    sRemote,
    5985,
    @"/wsman",
    @"http://schemas.microsoft.com/powershell/Microsoft.PowerShell";,
    creds);
ci.AuthenticationMechanism = auth;

Runspace runspace = RunspaceFactory.CreateRunspace(ci);
runspace.Open();
PowerShell psh = PowerShell.Create();
psh.Runspace = runspace;

我有两个问题,基于这段代码sn-p:

  1. 我需要长时间运行此会话;因此我需要确保远程会话处于活动状态。我该怎么做?
  2. 我可以更改会话保持活动的持续时间吗?

【问题讨论】:

  • 好问题 - 刚刚发布了答案。

标签: c# powershell powershell-remoting


【解决方案1】:

由于您有两个问题,我有两个标题分别回答。

PowerShell 运行空间状态

您可以使用指向RunSpaceState .NET enumeration 值的State 属性检查PowerShell Runspace 对象的状态:

var IsOpened = runspace.RunspaceStateInfo.State == RunspaceState.Opened;

设置空闲超时

如果您想为 PowerShell 会话设置 IdleTimeout,那么您需要:

  1. 实例化PSSessionOption
  2. 设置IdleTimeout属性
  3. WSManConnectionInfo 对象上调用SetSessionOptions() 方法

代码:

var option = new PSSessionOption();            // Create the PSSessionOption instance
option.IdleTimeout = TimeSpan.FromMinutes(60); // Set the IdleTimeout using a TimeSpan object
ci.SetSessionOptions(option);                  // Set the session options on the WSManConnectionInfo instance

【讨论】:

    猜你喜欢
    • 2011-03-14
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    相关资源
    最近更新 更多