0

is it possible to capture the Output from a commandline while a cmd is running? I´ve a small exe, which displays various Messages that should be processed with a script and displays the user some informations while the script is running.

My script starts the program (the exe) with some Parameters und checks if the process is still running. While the program is running i want to capture all messages to a variable to process it. I can´t find any solution, some tests with "run.exe 2>&1" etc... fails.

Any ideas?

logi
  • 3
  • 2
  • so in a powershell console $result = run.exe is not working for you? – Adil Hindistan Jun 05 '14 at 14:19
  • I`ve also tried (for example): `$result = Start-Process "ping.exe" -ArgumentList "localhost" -Wait $result` – logi Jun 05 '14 at 14:27
  • possible duplicate of [Capture EXE output in PowerShell](http://stackoverflow.com/questions/919171/capture-exe-output-in-powershell) – Loïc MICHEL Jun 05 '14 at 14:31
  • i´ve seen this script. but it captures the Output after the execution. I need the capture (each line) while it´s in progress. – logi Jun 05 '14 at 14:39
  • no, with Tee-Object i can´t access the Output while the exe is running. – logi Jun 06 '14 at 07:08

1 Answers1

0
$oInfo = New-Object System.Diagnostics.ProcessStartInfo
$oInfo.FileName  = "ping"
$oInfo.Arguments = "localhost"
$oInfo.UseShellExecute = $False
$oInfo.RedirectStandardOutput = $True

$oProcess = New-Object System.Diagnostics.Process
$oProcess.StartInfo = $oInfo

[Void]$oProcess.Start()

$bDone = $False

while (!$bDone)
{
    $char = $oProcess.StandardOutput.Read()

    if ($char -eq -1)
    {
        if ($oProcess.HasExited)
        {
            $bDone = $True
        }
        else
        {
            Wait-Event 1
        }
    }
    else
    {
        Write-Host -NoNewline "".PadLeft(1, $char)
    }
}
Antony
  • 450
  • 1
  • 4
  • 15