Hosting powershell runspace in web application Hosting powershell runspace in web application powershell powershell

Hosting powershell runspace in web application


Runspaces are not thread-safe, nor can they guarantee that the scripts they are running are either.

I would suggest you create a RunspacePool and have your web service queue work to it. This is actually pretty easy to do. I blogged about it for v2 ctp3, but the API has not changed for RTM.

http://www.nivot.org/2009/01/22/CTP3TheRunspaceFactoryAndPowerShellAccelerators.aspx

update:

If you want to preload each runspace in the pool with one or more modules, use the RunspaceFactory.CreateRunspacePool(InitialSessionState) overload. To see how to create and initialize this, see:

http://www.nivot.org/2010/05/03/PowerShell20DeveloperEssentials1InitializingARunspaceWithAModule.aspx

Every time you create a PowerShell instance, assign the pool to its RunspacePool property.