Since you have two questions, I have two headings for separate responses.
PowerShell Runspace State
You can examine the state of the PowerShell Runspace
object by using the State
property, which points to a value of the RunSpaceState
.NET enumeration:
var IsOpened = runspace.RunspaceStateInfo.State == RunspaceState.Opened;
Setting the IdleTimeout
If you want to set the IdleTimeout
for the PowerShell session, then you need to:
- Instantiate the
PSSessionOption
class - Set the
IdleTimeout
property - Call
SetSessionOptions()
method on theWSManConnectionInfo
object
Code:
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