What does the powershell property MaxConcurrentCommandsPerShell stand for?

I am writing an application where I spawn a persistent remote runspace.

Then for every request I create an object of System.Management.Automation.Powershell class, associate it with the runspace, add a powershell script and invoke the same. Once the script is invoked, I dispose the System.Management.Automation.Powershell class object. Here, I do not dispose, the runspace (powershell session).

After running my application for some time (>2-3 hours) the powershell call fails with an exception that says the quota for Maximum concurrent commands per shell has exceeded.

Since, I create a new instance of System.Management.Automation.Powershell class for every request, I was wondering how/why will the same script fail after 2-3 hours with the mentioned exception.

I wanted to understand, if there is something, I need to additionally dispose/cleanup?

I see MaxConcurrentCommandsPerShell is set to a reasonably high value of 1024, wanted to know how this limit is evaluated. I am executing a script in the shell and, do not think script commands will be executed concurrently, it would be rather sequential.

Can you please help if my understanding is incorrect somewhere?

Warm Regards

Himanshu Agarwal

July 28th, 2015 5:12pm

I suggest always startin with a search.

Here is one good source: http://blogs.technet.com/b/heyscriptingguy/archive/2013/07/30/learn-how-to-configure-powershell-memory.aspx

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 5:25pm

I have been looking for a good documentation for quite some days now, and have done a decent search. One of the search pointed to the link you've mentioned as well.

Here, I did not understand the difference between MaxMemoryPerShellMB and MaxConcurrentCommandsPerShell, the writer wants to increase the memory, but uses the command to increase the quota of the latter property, but the snapshot again displays, he writer executed the script to increase the memory of the shell. That, led me to believe, MaxConcurrentCommandsPerShell was a typo there for MaxMemoryPerShellMB.

The real question I have is why do I get the powershell exception "quota for maximum concurrent commands per shell has exceeded", when I am executing a script in the shell, which I believe is executed sequentially. And, all this happens when the same script is executed repeatedly, everytime in a new Powershell class object, after some 2-3 hours of run.



  • Edited by hemaanshu Tuesday, July 28, 2015 5:39 PM typo
July 28th, 2015 5:38pm

This is likely caused by recursive calls in a loop.

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 5:38pm

But, I do not have recursive calls in my powershell script.

July 28th, 2015 5:43pm

If you have a pipeline and it calls other commands which are pipelined you can have an implied recursion or an endless loop.

Gci * -dir | %{Gci $_ | ....}

or embedded loops within a pipeline.  All can create concurrent commands.  The setting is there to prevent infinite loops and infinite recursion.  Having mismatched parens or braces can also do this.

I suspect trying to cal linvoke-command inside an invoke-command might also do this.

Can't see your script so there is no way to answer your question exactly.

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 6:39pm

Thanks for your inputs, I'll be revisiting my scripts and traces.

Is there a possibility that powershell could already be logging this somewhere or can be made to do so? Maybe to an event log? Just a thought, I'll check the event logs as well.

July 29th, 2015 6:13am

I re-looked into scripts and everything looks fine there.

The same script executes fine initially but starts failing after 4-5 hours. Here's what I am doing for every script execution.

Collection<PSObject> result = null;

using (PowerShell powerShell = PowerShell.Create()) { // attach with our persistent runspace. powerShell.Runspace = this.remoteRunspace;   try     { powerShell.AddScript(PowerShellScriptLib.GetScript()); result = powerShell.Invoke(); }   catch (Exception ex) { this.TraceMsg("SCOM Powershell Exception: {0}", ex.Message); } }

if(null != result) { /* Process result */ }

And, I believe, this is not leaving any residue on the persistent runspace. I believe, "powershell" instance is collected/deleted after the using block, leaving the runspace in the same state as it was in before executing the script.

Free Windows Admin Tool Kit Click here and download it now
July 31st, 2015 1:59am

That is not a PowerShell script. Why would you claim it is?

You have one host and multiple runspaces.  We have noiea howyougot all of this upor what you have used to build it.

For developer issues you really need to post in the developer forum. 

July 31st, 2015 3:25am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics