none
display ping status line by line in multiline textbox for powershell winforms RRS feed

  • General discussion

  • Hi all,

    I am pinging an ip with -n = 10 and displaying the results to the textbox. But it is displaying the entire final output after the ping is completed to the textbox.

    But it should be displayed line by line in the textbox.

    $wks=$textfield2.Text; #we're taking the text from the input box into the variable $wks
        $pingResult= ping $wks -n 10 | fl | out-string;  #ping $wks
        $outputBox.text=$pingResult

    How to achieve it . Any help guys..................


    Naveen Basati

    Tuesday, December 8, 2015 11:44 AM

All replies

  • Hi Naveen,

    I'm guessing a bit here - you didn't fall all over yourself to provide much information on what and how you are trying to do - but this should probably do the trick:

    $wks = $textfield2.Text
    $pingResult = ping $wks -n 10 | out-string
    $outputBox.Multiline = $true
    $outputBox.text = $pingResult

    This worked just fine for me, anyway. If that doesn't do the trick though, well ... dunno, maybe you'd better add more context and give a few details on your system where you are running this (my crystal ball has been a bit cloudy recently).

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Tuesday, December 8, 2015 1:01 PM
  • Hi all,

    I am pinging an ip with -n = 10 and displaying the results to the textbox. But it is displaying the entire final output after the ping is completed to the textbox.

    But it should be displayed line by line in the textbox.

    $wks=$textfield2.Text; #we're taking the text from the input box into the variable $wks
        $pingResult= ping $wks -n 10 | fl | out-string;  #ping $wks
        $outputBox.text=$pingResult

    How to achieve it . Any help guys..................


    Naveen Basati

    There is no way to dynamically display the output of a utility command in a textbox. You can only display the total results of the command.

    You could use Test-Computer and a job and a timer t poll the query and incrementally display the results.    This is a very advanced forms technique and it doesn't seem to be useful for a ping.


    \_(ツ)_/

    Tuesday, December 8, 2015 2:19 PM
  • Hi Naveen,

    argh sorry, totally misunderstood your question.

    Contrary to what jrv said it actually is possible to peak into the output stream of an application like ping.exe. Here's an example:

    $lines = @()
    
    $ProcInfo = New-Object System.Diagnostics.ProcessStartInfo("ping.exe")
    $ProcInfo.Arguments = "$wks -n 10"
    $ProcInfo.RedirectStandardOutput = $true
    $ProcInfo.UseShellExecute = $false
    $ProcInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
    $Proc = New-Object System.Diagnostics.Process
    $Proc.StartInfo = $ProcInfo
    $Proc.Start() | Out-Null
    
    while (!$Proc.HasExited)
    {
    	$lines += $Proc.StandardOutput.ReadLine()
    	$outputBox.text = $lines -join "`n"
    }

    You need to redirect the outputstream and read from it line by line, as shown in this example.

    HOWEVER!

    If you do not perform this asynchroneously, the GUI will freeze ("Application is not responding" etc...). You'll still need to handle responsive GUI behavior yourself :)

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Tuesday, December 8, 2015 3:32 PM
  • It will not work inside of an event unless there is something that releases the event. A timer tick is useful rather than a loop.

    With this method the form will be frozen as long as it is pinging.


    \_(ツ)_/


    • Edited by jrv Tuesday, December 8, 2015 9:36 PM
    Tuesday, December 8, 2015 9:36 PM