0

I got some inspiration from here: Capturing standard out and error with Start-Process

What I want is to check if the standard output has some specific error messages and reopen the process.

My setup is as follows:

  • windows scheduled tasks that run a powershell script
  • inside the powershell script I run a bat file
  • the bat file runs a .NET executable

My script looks like this:

$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "Console.bat"
$pinfo.RedirectStandardError = $true
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = @("-s SomeArgument")

$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null   

$stdout=""

do
{
   $stdout += $p.StandardOutput.ReadLine()
}
while (!$p.HasExited)

if($stdout.Contains("Error while processing data"))
{
    Start-Sleep -Seconds 30;
    $p.Start() | Out-Null   
}

The problem is that when I run the scheduled tasks, I see cmd.exe and conhost.exe processes, I see the .NET exe running, I am forcing it to fail, in order to see the process being started again, but nothing. I can only see conhost.exe staying opened for 30 seconds, and than it closes. The process is not started again

Am I missing something? Thank you

Luci C
  • 197
  • 2
  • 3
  • 14

1 Answers1

0

Seems that I was missing something in the last part, I changed it to this and it works:

if($stdout.Contains("Error while processing data"))
{
Start-Sleep -Seconds 30;
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null   
}
Luci C
  • 197
  • 2
  • 3
  • 14