Powershell-Prozess Starten, Warten mit Timeout, Töten Sie und Holen Sie Exit-Code

Möchte ich wiederholt die Ausführung eines Programms in einer Schleife.

Manchmal stürzt das Programm ab, so will ich es töten, so dass die nächste iteration kann richtig beginnen. Ich bestimme dies über timeout.

Habe ich das timeout arbeiten, aber kann nicht den Exit-Code des Programms, die ich auch brauche, um zu bestimmen das Ergebnis.

Bevor ich nicht warten mit timeout, aber nur -warten in der Start-Prozess, aber dadurch wurde das Skript hängen bleibt, wenn das gestartete Programm abgestürzt. Mit diesem setup konnte ich richtig erhalten, der exit-code obwohl.

Bin ich der Ausführung von ISE.

for ($i=0; $i -le $max_iterations; $i++)
{
    $proc = Start-Process -filePath $programtorun -ArgumentList $argumentlist -workingdirectory $programtorunpath -PassThru
    # wait up to x seconds for normal termination
    Wait-Process -Timeout 300 -Name $programname
    # if not exited, kill process
    if(!$proc.hasExited) {
        echo "kill the process"
        #$proc.Kill() <- not working if proc is crashed
        Start-Process -filePath "taskkill.exe" -Wait -ArgumentList '/F', '/IM', $fullprogramname
    }
    # this is where I want to use exit code but it comes in empty
    if ($proc.ExitCode -ne 0) {
       # update internal error counters based on result
    }
}

Wie kann ich

  1. Einen Prozess starten,
  2. Warten, bis es ordentlich ausführen und fertig
  3. Töten, wenn es abgestürzt ist (e. g. trifft timeout)
  4. bekommen exit-code des Prozesses

InformationsquelleAutor Andreas Reiff | 2016-04-29

Schreibe einen Kommentar