I am using the System.Management.Automation
library to invoke PowerShell commands using a remote Runspace. So I create a PowerShell object and invoke a script as such:
var remoteRunspace = RunspaceFactory.CreateRunspace(wsmanConnectionInfo);
remoteRunspace.Open();
var powershell = PowerShell.Create();
var powershell.Runspace = remoteRunspace;
string PsScript = "Some long running powershell script"
powershell.AddScript(PsScript);
powerShell.Invoke();
The highlighted call is getting stuck and not coming out sometimes (like 1 out of 100 times, where we saw this thread getting stuck for more than 24 hours). If we restart the remote machine, then it comes out.
How do we fix this behavior? Is it a known issue in PowerShell? If yes will making it asynchronous using PowerShell.BeginInvoke
work? I am assuming that it should improve some reliability since it will queue it on a ThreadPool
thread.