Skip to content

Instantly share code, notes, and snippets.

@thecliguy
Last active March 4, 2018 11:01
Show Gist options
  • Save thecliguy/a55647176a1085c9aabea0aa28927f1a to your computer and use it in GitHub Desktop.
Save thecliguy/a55647176a1085c9aabea0aa28927f1a to your computer and use it in GitHub Desktop.
Find Expected Value in Standard Output
$ExpectedValue = "Ping statistics for 127.0.0.1:"
$Binary = "ping.exe"
$Arguments = "127.0.0.1 -n 10"
$ProcessStartInfo = New-Object System.Diagnostics.ProcessStartInfo
$ProcessStartInfo.UseShellExecute = $false
$ProcessStartInfo.RedirectStandardError = $true
$ProcessStartInfo.RedirectStandardInput = $true
$ProcessStartInfo.RedirectStandardOutput = $true
$ProcessStartInfo.FileName = $Binary
$ProcessStartInfo.Arguments = $Arguments
$Process = New-Object System.Diagnostics.Process
$Process.StartInfo = $ProcessStartInfo
$Process.Start() | Out-Null
Do {
$CurrentOutput = $null
$CurrentOutput = $process.StandardOutput.ReadLine()
$AllOutput = $AllOutput + "$CurrentOutput`n`r"
Start-sleep -Milliseconds 100
} While (!($CurrentOutput -match $ExpectedValue))
write-host "Expected value found: $CurrentOutput"
# If you want to see all the output produce within the loop plus that produced
# subsequently after exiting, uncomment the lines below:
####$AllOutput
####$process.StandardOutput.ReadToEnd()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment