We help IT Professionals succeed at work.

Running powershell scripts from ASP.NET

1,927 Views
Last Modified: 2018-05-22
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

Shaun VermaakSenior Consultant
CERTIFIED EXPERT
Awarded 2017
Distinguished Expert 2019

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

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 McFaddenTechnical Lead - Active Directory
CERTIFIED EXPERT

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

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.
Technical Lead - Active Directory
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

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