PowerShellからプログラムを実行し、終了を待ってからExitCodeにアクセスしようとしていますが、運がよくありません。バックグラウンドで実行するためにいくつかの処理が必要なため、で使用-Wait
したくありません。Start-Process
簡略化したテストスクリプトは次のとおりです。
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
このスクリプトを実行すると、メモ帳が起動します。これを手動で閉じた後、終了コードが出力され、を使用せずに再開され-wait
ます。これが終了すると、ExitCodeは提供されません。
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:
プログラムを開始してから終了するまでの間に追加の処理を実行できる必要があるため、を使用できません-Wait
。これを実行し、このプロセスから.ExitCodeプロパティにアクセスできるようにするにはどうすればよいですか?