我按照以下代码创建PowerShell Remoting会话:
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;
我有两个问题,基于此代码段:
答案 0 :(得分:3)
由于你有两个问题,我有两个标题可以单独回答。
您可以使用Runspace
属性检查PowerShell State
对象的状态,该属性指向RunSpaceState
.NET enumeration的值:
var IsOpened = runspace.RunspaceStateInfo.State == RunspaceState.Opened;
如果要为PowerShell会话设置IdleTimeout
,则需要:
PSSessionOption
类IdleTimeout
属性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