Answered by:
Need help for executing Powershell script from a ASP page

Question
-
User-2069132362 posted
I am not developer , trying to do small automation for my powershell script.
I have used below code to invoke the script. my aim is to Execute Powershell script alone. Result /output will handled by PS script.
I did not get any error when i run the page.
namespace Application
{
public partial class CallPsscript : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{}
protected void psbutton_Click(object sender, EventArgs e)
{
try
{
var shell = PowerShell.Create();
shell.Commands.AddScript("PowershellScript\test.ps1");
shell.Invoke();
}
catch(Exception ex)
{
}}
}
}MY Powershell script will create a output file for the result
Tuesday, May 21, 2019 3:57 PM
Answers
-
User-1174608757 posted
Hi manusan,
According to your description, you should add a virtual path in Server.MapPath() method. You should firstly save test.ps1 in the same folder with your asp page in your application. Then you could use server.MapPath() to find it physical path. You could see as below:
string sourceFile = Server.MapPath("/test.ps1");
Here is the link,I hope it could help you.
Best Regards
Wei
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 22, 2019 7:04 AM
All replies
-
User753101303 posted
Hi,
catch(Exception ex)
{
}
will just ignore any error that could happen. Just get rid of that...Where is your script ? For a web app, it is likely the "current directory" is not what you expect. If PowershellScript is a folder under your web site try :
shell.Commands.AddScript(Server.MapPath("/~PowershellScript/test.ps1"));
Server.MapPath will return something such as "c:\somewhere\yoursiteroofolder\PowershellScript\test.ps1" ie it get the physical path for a a file on your site.
Tuesday, May 21, 2019 4:24 PM -
User-2069132362 posted
Changed the code and getting the error as below
[HttpException (0x80004005): 'D:/Inetpub/wwwroot/PowershellScript/test.ps1' is a physical path, but a virtual path was expected.] System.Web.Util.UrlPath.CheckValidVirtualPath(String path) +9830644 System.Web.Util.UrlPath.Combine(String appPath, String basepath, String relative) +132
string sourceFile = Server.MapPath(@"D:\Inetpub\wwwroot\PowershellScript\test.ps1");
var shell = PowerShell.Create();
shell.Commands.AddScript(Server.MapPath("/~PowershellScript/test.ps1"));
shell.Invoke();Wednesday, May 22, 2019 3:13 AM -
User-1174608757 posted
Hi manusan,
According to your description, you should add a virtual path in Server.MapPath() method. You should firstly save test.ps1 in the same folder with your asp page in your application. Then you could use server.MapPath() to find it physical path. You could see as below:
string sourceFile = Server.MapPath("/test.ps1");
Here is the link,I hope it could help you.
Best Regards
Wei
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, May 22, 2019 7:04 AM -
User753101303 posted
Server.MapPath(@"D:\Inetpub\wwwroot\PowershellScript\test.ps1") doesn't make sense. You provide a path relative to your web app and it returns its physical path ie :
Server.MapPath("/~PowershellScript/test.ps1") should return something such as "D:\Inetpub\wwwroot\PowershellScript\test.ps1" so it could be :
string sourceFile = Server.MapPath("/~PowershellScript/test.ps1");
var shell = PowerShell.Create();
shell.Commands.AddScript(sourceFile);
shell.Invoke();Or you could populate sourceFile directly from some configurartion information (or the root directorty for your ps scripts) if you store them at some other place which is not under the web site root.
Wednesday, May 22, 2019 1:31 PM -
User-2069132362 posted
Thank you All ,its working now.
Thursday, May 23, 2019 1:21 AM