Solved

StartInfo.RedirectStandardOutput   To a richtext box

Posted on 2013-11-14
1
534 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
ID: 39651070
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 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