Solved

StartInfo.RedirectStandardOutput   To a richtext box

Posted on 2013-11-14
1
524 Views
Last Modified: 2013-11-15
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
Comment
Question by:AjarnJonesy
1 Comment
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now