Solved

StartInfo.RedirectStandardOutput   To a richtext box

Posted on 2013-11-14
1
540 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

690 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