views:

47

answers:

3

Is there any problems with creating a powershell runspace in an asp.net application and running commands?

I have a basic example working fine but I'm wondering if there are any pitfalls waiting for me.

I'm particularly wondering:

  • Is this an a light-weight inprocess operation or is it firing up another processes that could lead to unpredictable behaviour.
+1  A: 

This should be OK. I've not done this in ASP.NET but I have hosted in-process with a desktop app. There is no external PowerShell process spun up unless you use background jobs. I think the Quest folks are also doing something like this with their Mobile Shell.

Keith Hill
ha-ha! I win. :D
x0n
+3  A: 

As long as you're not shelling out to powershell.exe explicitly, instead using Runspace and Pipeline objects directly, you can be assured it's fairly lightweight. Btw, if you are trying to interact with cmdlets through C#, only cmdlets that derive from PSCmdlet need a pipeline; ones that derive from Cmdlet directly can be called without a pipeline via the Invoke method. This is the most lightweight approach.

x0n
+1  A: 

As I can see, others recommend using PowerShell. Well, personally, I would pay more attention. Why?

Each web request should be processed as quickly as possible and without any blocking. If the script contains commands that work with network, then there could be some timeouts (e.g. if the computer is not accessible). The processing thread will be blocked for all the time and can't serve any other web request. Then soon you might get Internal Server Error and similar responses.

Besides that (not proved) I suspect that PowerShell consumes more memory than similar code in C#.

I don't claim don't use Powershell, just pay attention ;)

stej
thanks @stej. all good points. Unfortunately running a powershell command seems to be the only "supported" way to do what I would like to do. The alternative I had considered was having the web request add to a queue so the powershell was called async and in a windows service. Unfortunately the user is going to be waiting for a response so the only advantage in our case would be to free up web server threads earlier and use ajax to poll for a result.
Brownie