Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I have a C# component which executes Powershell scripts in powershell environment. Suddenly it started to show some issues, Now, when I try to execute the following, it shows exception

PHP
Enter-PSSession -ComputerName [IP] -Credential abc\admin


The exception is given below...

XML
Enter-PSSession : Connecting to remote server failed with the following error message : 
    The WS-Management service cannot process the request. **This user is allowed a maximum 
    number of 5 concurrent shells, which has been exceeded.** Close existing shells or raise 
    the quota for this user. For more information, see the about_Remote_Troubleshooting 
    Help topic.
    At line:1 char:16
    + Enter-PSSession <<<<  -ComputerName [IP] -Credential abc\admin
        + CategoryInfo          : InvalidArgument: ([IP]:String) [Enter-PSSession], 
    PSRemotingTransportException
        + FullyQualifiedErrorId : CreateRemoteRunspaceFailed


I fear, this is because I haven't properly closed / disposed the runspaces created in c# component.
Here is the code extracted from my component

C#
......
	Collection<PSObject> objPS = new Collection<PSObject>();
    PowerShell powershell = PowerShell.Create();
	PSCredential credential = new PSCredential(strUserName, password);
    objRunspace = RunspaceFactory.CreateRunspace();
	objRunspace.Open();
	objPS = powershell.Invoke();
	......
	finally 
            {
                objRunspace.Dispose();
			}


I am beating the bush, instead of finding the root cause of this issue.

Thanks...
Posted
Updated 5-Nov-14 22:39pm
v2
Comments
Nathan Minier 6-Nov-14 9:02am    
Is there an explicit logout that Dispose might not be calling?

Is the code block in a loop?
sjelen 6-Nov-14 9:41am    
try calling objRunspace.Close() explicitly.
Since both PowerShell and Runspace implement IDisposable you can use 'using' block
using (var powershell = PowerShell.Create()){
...
using (var objRunspace = RunspaceFactory.CreateRunspace()){
...

}
}
Then you don't have to worry about calling .Dispose() it'll be called automatically when execution exit using block.
Sebastian T Xavier 10-Nov-14 3:44am    
Thanks..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900