?
Solved

StartInfo.RedirectStandardOutput   To a richtext box

Posted on 2013-11-14
1
Medium Priority
?
546 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 2000 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: 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.

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)…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

762 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