AjarnJonesy
asked on
Call external script from C# code
Have built a simple page with drop down and submit button. Basically it is a simple maintenance job that will call external scripts located on the same server as the aspx page itself. The whole idea of this is to give my DBA team the ability to run a maint script through the browser. Any way what I am trying to do is with the onclick event/selection run an external .bat or .ps1 which is located on the same server.
I am wondering if I can use something like:
Process proc = new process();
proc.StartInfo.Filename = "c:\scripts\Maintstart.ps1 ");
proc.StartInfo.WorkingDire ctory = @"c:\scripts\";
proc.Start()
My default.aspx.cs page is where I am trying to keep all of this. If I use the above do I need to put it in both if statements? and how do I define the runspace for it?
using System;
using System.Collections.Generic ;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MaintApp
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedInde xChanged(o bject sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.DropDownList1.Select edValue == "Maint Window Start")
{
(this is where I want to call the start script from
}
if (this.DropDownList1.Select edValue == "Maint Window Complete")
{
(this is where I want to call the stop script from)
}
}
}
}
I am wondering if I can use something like:
Process proc = new process();
proc.StartInfo.Filename = "c:\scripts\Maintstart.ps1
proc.StartInfo.WorkingDire
proc.Start()
My default.aspx.cs page is where I am trying to keep all of this. If I use the above do I need to put it in both if statements? and how do I define the runspace for it?
using System;
using System.Collections.Generic
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MaintApp
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedInde
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.DropDownList1.Select
{
(this is where I want to call the start script from
}
if (this.DropDownList1.Select
{
(this is where I want to call the stop script from)
}
}
}
}
ASKER
This will be run from the server side. The page sits in c:\inetpub\wwwroot\maintsi te and the scripts sit on C:\scripts\___.ps1.. the only client side interaction I am looking for is the ability to hit the submit button.
I have added the page and .cs file .. Hopefully that make s more sense with what I am doing.
thanks
default.aspx
default.aspx.cs
I have added the page and .cs file .. Hopefully that make s more sense with what I am doing.
thanks
default.aspx
default.aspx.cs
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Perfect.. Thanks Kaufmed.
I have it added in and just need to get the correct system.diagnostics references added ...
I have it added in and just need to get the correct system.diagnostics references added ...
ASKER
I added the above into my default.aspx.cs file. I also added using System.Diagnostics as well as making sure system was listed in references. However when I do a build I receive the following error.
Error 9 'System.Diagnostics.Proces sStartInfo ' does not contain a definition for 'Filename' and no extension method 'Filename' accepting a first argument of type 'System.Diagnostics.Proces sStartInfo ' could be found (are you missing a using directive or an assembly reference?) C:\Code\Temp\MaintApp\Main tApp\defau lt.aspx.cs 27 28 MaintApp
When I look at the references for System I see ProcessStartinfo is part of it. Also I noticed that ProcessStartInfo only References Base Types > Object. So it does look like the definition for Filename is not there.
Here is the code.
using System;
using System.Diagnostics;
using System.Collections.Generic ;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MaintApp
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedInde xChanged(o bject sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Process proc = new Process();
proc.StartInfo.Filename = null;
proc.StartInfo.WorkingDire ctory = @"c:\scripts";
if (this.DropDownList1.Select edValue == "Maint Window Start")
{
proc.StartInfo.Filename = @"C:\Scripts\StopSVCFinal. ps1";
}
else if (this.DropDownList1.Select edValue == "Maint Window Complete")
{
proc.StartInfo.Filename = @"C:\Scripts\StartSVCFinal .ps1";
}
if (proc.StartInfo.Filename != null)
{
proc.Start();
}
}
}
}
Error 9 'System.Diagnostics.Proces
When I look at the references for System I see ProcessStartinfo is part of it. Also I noticed that ProcessStartInfo only References Base Types > Object. So it does look like the definition for Filename is not there.
Here is the code.
using System;
using System.Diagnostics;
using System.Collections.Generic
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MaintApp
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedInde
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Process proc = new Process();
proc.StartInfo.Filename = null;
proc.StartInfo.WorkingDire
if (this.DropDownList1.Select
{
proc.StartInfo.Filename = @"C:\Scripts\StopSVCFinal.
}
else if (this.DropDownList1.Select
{
proc.StartInfo.Filename = @"C:\Scripts\StartSVCFinal
}
if (proc.StartInfo.Filename != null)
{
proc.Start();
}
}
}
}