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 :
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.
[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();
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:
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.
Member
2 Points
10 Posts
Need help for executing Powershell script from a ASP page
May 21, 2019 03:57 PM|manusan|LINK
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
All-Star
48500 Points
18071 Posts
Re: Need help for executing Powershell script from a ASP page
May 21, 2019 04:24 PM|PatriceSc|LINK
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.
Member
2 Points
10 Posts
Re: Need help for executing Powershell script from a ASP page
May 22, 2019 03:13 AM|manusan|LINK
Changed the code and getting the error as below
string sourceFile = Server.MapPath(@"D:\Inetpub\wwwroot\PowershellScript\test.ps1");
var shell = PowerShell.Create();
shell.Commands.AddScript(Server.MapPath("/~PowershellScript/test.ps1"));
shell.Invoke();
Participant
1300 Points
522 Posts
Re: Need help for executing Powershell script from a ASP page
May 22, 2019 07:04 AM|Wei Zhang|LINK
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:
Here is the link,I hope it could help you.
https://docs.microsoft.com/en-us/dotnet/api/system.web.httpserverutility.mappath?view=netframework-4.8
Best Regards
Wei
All-Star
48500 Points
18071 Posts
Re: Need help for executing Powershell script from a ASP page
May 22, 2019 01:31 PM|PatriceSc|LINK
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.
Member
2 Points
10 Posts
Re: Need help for executing Powershell script from a ASP page
May 23, 2019 01:21 AM|manusan|LINK
Thank you All ,its working now.