Running powershell scripts from ASP.NET

ukkaapie
ukkaapie used Ask the Experts™
on
Hi,

I am trying to run a powershell script from an ASP.NET page.

I have configured the application pool to run under a doman account identity but the powershell script appears to be running under the machine account.

When I add $env:username to the powershell script, it returns computername$

Any ideas where I am going wrong?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

Commented:
Under what account is you AppPool running?
CoralonSenior Citrix Engineer

Commented:
As Shaun is pointing out.. the scripts are going to run in the context of the AppPool that is running the website.  I'm not an IIS expert, (I have some experience with it).. I believe you might be able to request credentials from the user and use those in your powershell scripts.  I'm not sure how or if you could capture any kind of passthrough authentication.

Coralon
Dan McFaddenSystems Engineer

Commented:
This is do-able as per the example C# WinForm article below states... should be similar for ASP.NET.

Links:
1. https://blogs.msdn.microsoft.com/zainnab/2008/07/26/calling-a-powershell-script-from-your-net-code/
2. http://jeffmurr.com/blog/?p=142

The important thing to do is use the System.Management.Automation name space instead of calling a command.

Dan
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Author

Commented:
@Shaun - The application pool is running under a domain account.

@Dan - That namespace is being used to call the script and not a command.
Systems Engineer
Commented:
By default, an IIS AppPool does not load the user profile when that account is setup as the AppPool's Identity.

I would setting up the AppPool to load the profile.  In IIS Manager, do the following:

1. Select the AppPool that the website is running under
2. Right click it and select Advanced Settings
3. Under the "Process Model" section, change "Load User Profile" from false to true.
4. For safety purposes, do an IISRESET.
5. Verify functionality.

Dan

Author

Commented:
Dan...

Thanks heaps for that.  Completely forgot but I had to do that for a website belonging to someone else.

Excellent... all working great now...

Shane

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial