4

I am connecting to remote computers from C# application using PowerShell Remoting (directly from C# code). That works fine, however every such connection leaves a new instance of process wsmprovhost.exe (WSMan host process) lingering on the target computer. They never exit and consume small amounts (<0.1%) of CPU time.

Here's sample code which I use to open PowerShell sessions:

 WSManConnectionInfo ci = new WSManConnectionInfo(
       false, host, 5985, 
       "/wsman", "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", 
       credential
 );
 ci.AuthenticationMechanism = AuthenticationMechanism.Credssp;
 runspace = RunspaceFactory.CreateRunspace(ci);
 runspace.ApartmentState = Thread.CurrentThread.GetApartmentState();
 runspace.Open();

... and here's how currently I close that session:

 runspace.Disconnect();
 runspace.Close();
 runspace.Dispose();

I ruled out possibility that anything that we do in this runspace causes this, because wsmprovhost.exe stays there even we don't do anything with it - i.e. simply connect and then disconnect.

The problem does not happen when I do equivalent action manually from PS command line - i.e:

Enter-PSSession $host -Credential $cred -Authentication CredSSP
...
Exit-PSSession

In the latter case, an instance of wsmprovhost.exe appears after Enter-PSSession, and then closes immediately after Exit-PSSession.

I tried the following things so far, none of which helped:

  • Using RunspacePool instead of Runspace
  • All combinations and permutations of Disconnect/Close/Dispose
  • Setting CancelTimeout on WSManConnectionInfo to small values (1ms)

Am I missing something simple ? What is the correct way of closing programmatically opened PowerShell sessions ?


Edit: Windows 7, PowerShell 3.0 and .Net 4.0 on all systems.

Edit 2: I found that setting IdleTimeout on WSManConnectionInfo to 60000 (60 seconds - lowest allowed value) makes wsmprovhost.exe exit one minute after disconnection. However, in command-line scenario, IdleTimeout is 2 hours, and yet wsmprovhost.exe terminates immediately after disconnect. Also, I now noticed that wsmprovhost.exe started in response to C# actions does exit after all, but only in 2 hours. One minute is certainly better than 2 hours, but in heavy-load scenarios, our application may have to run PowerShell frequently enough for 1-minute closure time to be a problem.

Cozzamara
  • 1,318
  • 1
  • 14
  • 22
  • What are the source and target operating systems? What version of PowerShell on each? –  Mar 13 '14 at 03:49
  • Windows 7, PowerShell 3.0 and .Net 4.0 on all systems – Cozzamara Mar 13 '14 at 04:16
  • @Knuckle-Dragger call Flush() on which object ? Output stream ? Something else ? There are no streams or anything else besides Runspace in open-and-close scenario – Cozzamara Mar 13 '14 at 19:55

0 Answers0