Brownie
Brownie

Reputation: 7738

Is it ok to call Powershell from an ASP.Net process

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:

Upvotes: 1

Views: 385

Answers (3)

stej
stej

Reputation: 29479

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 ;)

Upvotes: 1

x0n
x0n

Reputation: 52480

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.

Upvotes: 3

Keith Hill
Keith Hill

Reputation: 202052

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.

Upvotes: 1

Related Questions