如何检查 `&` Powershell 命令的错误代码?

How do I check the errorcode of a `&` Powershell command?

在 Powershell 脚本中,我 运行 命令如下:

& "C:\...\myprogram.exe"

如何查看 myprogram.exe 是否返回了错误代码?

如果您 运行 使用呼叫操作员(或不使用),请选中 $LASTEXITCODE。它将始终是您在当前会话中 运行 最后一个程序的退出代码。

如果您需要使用 Start-Process 代替(例如需要使用 -Wait 等待 GUI 应用程序完成),请确保使用 -PassThru 来存储过程对象作为变量,然后检查它的 ExitCode 属性。 $LASTEXITCODE 在这种情况下未设置。