views:

72

answers:

1

I suppose my question is somewhat similar to this: http://stackoverflow.com/questions/2607865/redirecting-the-standard-output-input-error-into-from-a-textbox but it is powershell-centric.

I'm working on an GUI created using PrimalForms that will act as a frontend to my website deployment scripts. Up until now, I've been using start-transcript and stop-transcript to update my textbox in the gui which acts as a console to view the events of the script process.

However, I'm now intending on using a script for our load balancer which is more time-sensitive. I need the text-box to be able to show the output (which is directed to write-host by said script) in real-time or close to it. Capturing a logfile/transcript after the fact simply won't do.

I've been reading up on 'Understanding Output' and all the articles I can find, but I'm not seeing a method that will really work for my needs. I was hoping there was simply a 'set-host' commandlet I could use and direct all my write-host entries to it.

Any ideas?

Edit: I've considered piping to a textfile with out-string and periodically updating from that, but it seems like a real kludge.

Edit2: hmm, maybe Tee-object... http://stackoverflow.com/questions/3463211/powershell-how-to-capture-output-from-the-host

Edit3: Okay, I'm almost there with:

ping -n 10 127.0.0.1 | out-string -Stream | foreach-object {$richTextBox1.lines = $richTextBox1.lines + $_}

but it seems to hang rather than stream.

A: 

Okay, I believe I've found a workable solution. The issue with my final edit above was not that out-string wasn't streaming, but that I wasn't refreshing the form upon each additional line.

Here's a sample of the idea for those who are interested (I've included a couple lines to auto-scroll the textbox as well):

ping -n 10 127.0.0.1 | out-string -Stream | foreach-object {
    $richTextBox1.lines = $richTextBox1.lines + $_
    $richTextBox1.Select($richTextBox1.Text.Length, 0)
    $richTextBox1.ScrollToCaret()
    $form1.Update()}

The solution for trapping Write-Host is slightly different; essentially logging from another powershell instance:

Powershell.exe -noprofile -file .\psbackup.ps1 $txtbox_codePath.text $comboBox_environment.selecteditem | Out-String -Stream | ForEach-Object {
    $txtBox_console.Lines = $txtBox_console.Lines + $_
    $txtBox_console.Select($txtBox_console.Text.Length, 0)
    $txtBox_console.ScrollToCaret()
    $form1.Update()
}
Yanagi
**NOTE** This doesn't actually solve the problem of grabbing the write-host statements. Only objects are piped to string and then the makeshift console.
Yanagi