Try this:
function Read-ConsoleOutput
{
param (
[System.Diagnostics.Process]$Process,
[String]$Command,
[Int32]$Timeout
)
$Process.StandardInput.WriteLine($Command)
$output = Wait-Prompt -Process $Process -Prompt $prompt -Timeout $Timeout
return $output
}
function Wait-Prompt
{
param (
[System.Diagnostics.Process]$Process,
[String]$Prompt,
[Int32]$Timeout
)
$output = [String]::Empty
$timer = New-Timer -Seconds $Timeout
$timer.Start()
do
{
while ($Process.StandardOutput.Peek() -ge 0)
{
$char = [Char]$Process.StandardOutput.Read()
$output += $char
}
}
until ($output.Contains($Prompt) -or $timer.HasElapsed -or $Process.HasExited)
$timer.Dispose()
return $output
}
function New-Timer
{
param (
[Int32]$Seconds
)
$timer = New-Object System.Timers.Timer
$timer.AutoReset = $false
$timer.Interval = [Double]($Seconds * 1000)
$timer | Add-Member -MemberType NoteProperty -Name HasElapsed -Value $false
$TimerElapsed = {
param (
[Object]$Sender,
[System.Timers.ElapsedEventHandler]$EventArgs
)
$Sender.HasElapsed = $true
$Sender.Stop()
}
Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier Timer.Elapsed -Action $TimerElapsed
$TimerDisposed = {
param (
[Object]$Sender,
[System.Timers.ElapsedEventHandler]$EventArgs
)
Unregister-Event -SourceIdentifier Timer.Elapsed
Unregister-Event -SourceIdentifier Timer.Disposed
}
Register-ObjectEvent -InputObject $timer -EventName Disposed -SourceIdentifier Timer.Disposed -Action $TimerDisposed
return $timer
}
$output = Read-ConsoleOutput -Process $exe -Command $prompt -Timeout 600