1

I created a little WindowsService that uses a RunspacePool and executes PowerShell scripts with the associated Runspaces on request. This works great, but when the service doesn't get any requests for about 4.5 hours the scripts are not running anymore. The PowerShell shows that it has an error, but the Error-Stream is empty. Even building up a new RunspacePool after 3 hours did not work. I have no idea what the problem might be. Any help would be highly appreciated.

That's the code to create the 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>();
4

0 回答 0