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?

5
  • so in a powershell console $result = run.exe is not working for you? Commented Jun 5, 2014 at 14:19
  • Ive also tried (for example): $result = Start-Process "ping.exe" -ArgumentList "localhost" -Wait $result` Commented Jun 5, 2014 at 14:27
  • possible duplicate of Capture EXE output in PowerShell Commented Jun 5, 2014 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. Commented Jun 5, 2014 at 14:39
  • no, with Tee-Object i can´t access the Output while the exe is running. Commented Jun 6, 2014 at 7:08

1 Answer 1

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)
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.