• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 560
  • Last Modified:

StartInfo.RedirectStandardOutput To a richtext box

I have a small webpage I put together which has 4 options in a dropdown box. What I am doing is choose and option then click Submit. On clicking submit it invokes a specific powershell script. What I am trying to do now is use StartInfor.RedirectStandardOutput/error to a rich textbox on the page screen.  Simply put I just want to see every step the script is doing on my remote machines.. Since I will be using my pc to open this page in a browser (the page/site/scripts are all on a webserver) I have used the following so that it doesn't try to open a command prompt while the .ps1's are running. I would like to put this into a Richtextbox instead.
Here is the code I currently have in my default.aspx.cs file.

       {
    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 = @"Powershell.exe";
                proc.StartInfo.Arguments = @"& 'C:\Scripts\StopSVCFinal.ps1'";
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
            }
            if (this.DropDownList1.SelectedValue == "Maint Window Complete")
            {
                proc.StartInfo.FileName = @"Powershell.exe";
                proc.StartInfo.Arguments = @"& 'C:\Scripts\StartSVCFinal.ps1'";
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
            }
            if (this.DropDownList1.SelectedValue == "SQL Restart Begin")
            {
                proc.StartInfo.FileName = @"Powershell.exe";
                proc.StartInfo.Arguments = @"& 'C:\Scripts\StartSQLFinal.ps1'";
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
            }
            else if (this.DropDownList1.SelectedValue == "SQL Restart Complete")
            {
                proc.StartInfo.FileName = @"Powershell.exe";
                proc.StartInfo.Arguments = @"& 'C:\Scripts\StartSQLFinal.ps1'";
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.CreateNoWindow = true;
             
            }
            if (proc.StartInfo.FileName != null)
            {
                proc.Start();
             
            }

        }
    }
}

Here is what  I am a bsic overview of what I am trying to do..

         {
          proc.Start();
          proc.BeginOutputReadLine();
          proc.WaitForExit();

         }

}
Void SortOutputHandler(object sender, DataReceivedEventArgs e)

    {
       Trace.WriteLine(e.Data);
        this.BeginInvoke( new MethodInvoker(() =>
    {
        RichTextBox1.AppendText(e.Data ?? string.Empty);
       }));
     }
0
AjarnJonesy
Asked:
AjarnJonesy
1 Solution
 
Bob LearnedCommented:
Here are my observations about your question:

1) You have a lot of duplicated code in the if...else block.  

2) You might want to use a switch block to determine the script to run, since it is the only element that depends on the option selected.

3) You can use the Process.StandardOutput stream reader to get the output:

Process.StandardOutput Property
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx

// Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now