Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources: Active Threads
Viewing all articles
Browse latest Browse all 8411

Capture all process output and display it

$
0
0

Hi all

Going round in circles here, help please.

I'm wanting to capture the out put of an exe (so I can email it after) and also display it. Am I missing something obvious?

So here's an example of how far I've got with a good old ping.

$Process = New-Object system.Diagnostics.Process
$Process.StartInfo = New-Object System.Diagnostics.ProcessStartInfo("ping","127.0.0.1")

$Process.StartInfo.RedirectStandardOutput = $true
$Process.StartInfo.RedirectStandardError = $true
$Process.StartInfo.UseShellExecute = $false

$Process.Start() | Out-Null

echo "Live output, missing last out put..."

while (!$Process.HasExited){
   Write-Host $Process.StandardOutput.ReadLine()
}

$stdout = $Process.StandardOutput.ReadToEnd()
$stderr = $Process.StandardError.ReadToEnd()

echo "`nI want to show all out put in variables not just the end...`n"

Write-Host "stdout: $stdout"
Write-Host "stderr: $stderr"

This out puts this though. I want it to both out put everything as it goes along and also save everything to the variable to email later:

  • the final out put is not displayed as it goes along (as it HasExited)
  • ReadToEnd is just showing what was missed on point 1
Live output, missing last out put...

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

I want to show all out put in variables not just the end...

stdout:
Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

stderr:

Thank you!


Viewing all articles
Browse latest Browse all 8411

Latest Images

Trending Articles



Latest Images