gpt4 book ai didi

c# - 在 C# 中检查远程 PowerShell session 状态

转载 作者:行者123 更新时间:2023-11-30 22:09:55 24 4
gpt4 key购买 nike

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

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;

我有两个问题,基于这个代码片段:

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

最佳答案

既然你有两个问题,我有两个标题分别回答。

PowerShell 运行空间状态

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

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

设置空闲超时

如果要为 PowerShell session 设置 IdleTimeout,则需要:

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

代码:

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

关于c# - 在 C# 中检查远程 PowerShell session 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21210568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com