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)
            }
         


        }

   
   
    }
}
AjarnJonesyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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?
0
AjarnJonesyAuthor Commented:
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
0
käµfm³d 👽Commented:
You don't have to put all of the logic in both if blocks. You can put the part that can change within the if--the filename.

e.g.

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\Maintstart.ps1";
    }
    else if (this.DropDownList1.SelectedValue == "Maint Window Complete")
    {
       proc.StartInfo.Filename = @"c:\scripts\Maintstop.ps1";
    }
    
    if (proc.StartInfo.Filename != null)
    {
        proc.Start();
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AjarnJonesyAuthor Commented:
Perfect.. Thanks Kaufmed.
I have it added in and just need to get the correct system.diagnostics references added ...
0
AjarnJonesyAuthor Commented:
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();
            }


        }

   
   
    }
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.