Link to home
Start Free TrialLog in
Avatar of AjarnJonesy
AjarnJonesyFlag for United States of America

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.WorkingDirectory = @"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_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.DropDownList1.SelectedValue == "Maint Window Start")
            {
               (this is where I want to call the start script from
            }
            if (this.DropDownList1.SelectedValue == "Maint Window Complete")
            {
               (this is where I want to call the stop script from)
            }
         


        }

   
   
    }
}
Avatar of kaufmed
kaufmed
Flag of United States of America image

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
Run it where? On the server, or on the client?
Avatar of AjarnJonesy

ASKER

This will be run from the server side. The page sits in c:\inetpub\wwwroot\maintsite 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
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perfect.. Thanks Kaufmed.
I have it added in and just need to get the correct system.diagnostics references added ...
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.ProcessStartInfo' does not contain a definition for 'Filename' and no extension method 'Filename' accepting a first argument of type 'System.Diagnostics.ProcessStartInfo' could be found (are you missing a using directive or an assembly reference?)      C:\Code\Temp\MaintApp\MaintApp\default.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_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)

        {
            Process proc = new Process();
            proc.StartInfo.Filename = null;
            proc.StartInfo.WorkingDirectory = @"c:\scripts";

            if (this.DropDownList1.SelectedValue == "Maint Window Start")
            {
                proc.StartInfo.Filename = @"C:\Scripts\StopSVCFinal.ps1";
            }
            else if (this.DropDownList1.SelectedValue == "Maint Window Complete")
            {
                proc.StartInfo.Filename = @"C:\Scripts\StartSVCFinal.ps1";
            }
            if (proc.StartInfo.Filename != null)
            {
                proc.Start();
            }


        }

   
   
    }
}