Erhalten Sie ExitCode mit Start-Process und WaitForExit anstelle von -Wait
Ich versuche, ein Programm von PowerShell aus auszuführen, auf den Exit zu warten und dann auf den ExitCode zuzugreifen, aber ich habe nicht viel Glück. Ich möchte nicht verwenden-Wait
mitStart-Process
, da ich etwas Verarbeitung benötige, um im Hintergrund weiterzumachen.
Hier ist ein vereinfachtes Testskript:
<code>cd "C:\Windows" # ExitCode is available when using -Wait... Write-Host "Starting Notepad with -Wait - return code will be available" $process = (Start-Process -FilePath "notepad.exe" -PassThru -Wait) Write-Host "Process finished with return code: " $process.ExitCode # ExitCode is not available when waiting separately Write-Host "Starting Notepad without -Wait - return code will NOT be available" $process = (Start-Process -FilePath "notepad.exe" -PassThru) $process.WaitForExit() Write-Host "Process exit code should be here: " $process.ExitCode </code>
Das Ausführen dieses Skripts wird dazu führenNotizblock gestartet werden. Nachdem dies manuell geschlossen wurde, wird der Beendigungscode gedruckt und ohne Verwendung von neu gestartet-wait
. Es wird kein ExitCode bereitgestellt, wenn dieser beendet wird:
<code>Starting Notepad with -Wait - return code will be available Process finished with return code: 0 Starting Notepad without -Wait - return code will NOT be available Process exit code should be here: </code>
Ich muss in der Lage sein, zwischen dem Starten des Programms und dem Warten auf das Beenden des Programms eine zusätzliche Verarbeitung durchzuführen, damit ich nicht darauf zurückgreifen kann-Wait
. Wie kann ich das tun und trotzdem von diesem Prozess aus auf die .ExitCode-Eigenschaft zugreifen?