我创建了一个使用RunspacePool的WindowsService,并根据请求执行PowerShell脚本和相关的Runspaces。这很好用,但是当服务没有得到大约4.5小时的任何请求时,脚本就不再运行了。 PowerShell显示它有错误,但错误流是空的。甚至在3小时后建立一个新的RunspacePool也行不通。我不知道问题可能是什么。任何帮助都将受到高度赞赏。
这是创建runspacepool的代码:
Uri uri = credentials.ConnectionUri;
SecureString securePassword = credentials.Password;
PSCredential creds = new PSCredential(credentials.UserName, securePassword);
InitialSessionState iss = InitialSessionState.CreateDefault();
RunspacePool runspacePool = RunspaceFactory.CreateRunspacePool(iss);
runspacePool.ThreadOptions = PSThreadOptions.UseNewThread;
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand(string.Format("New-PSSession"));
command.AddParameter("ConfigurationName", "Microsoft.Exchange");
command.AddParameter("ConnectionUri", uri);
command.AddParameter("Credential", creds);
command.AddParameter("Authentication", "Basic");
command.AddParameter("AllowRedirection");
PSSessionOption sessionOption = new PSSessionOption();
sessionOption.SkipCACheck = true;
sessionOption.SkipCNCheck = true;
sessionOption.SkipRevocationCheck = true;
command.AddParameter("SessionOption", sessionOption);
powershell.Commands = command;
runspacePool.Open();
powershell.RunspacePool = runspacePool;
Collection<PSSession> result = powershell.Invoke<PSSession>();