你能举例说明如何使用PowerShell在出错时返回错误代码1吗?
例如,处理这种情况:
if ($serviceUserName) {
cmd /c $serviceBinaryFinalPath install -username:$serviceUserName -password:$serviceUserPassword -servicename:"$ServiceName" -displayname:"$serviceDisplayName" -description:"$serviceDescription"
} else {
cmd /c $serviceBinaryFinalPath install --localsystem -servicename:"$ServiceName" -displayname:"$serviceDisplayName" -description:"$serviceDescription"
}发布于 2017-01-17 23:01:59
您正在运行外部命令,因此需要检查automatic variable $LastExitCode以检测错误:
if ($LastExitCode -ne 0) {
exit 1
}或者使用最后一个外部命令的退出代码退出:
exit $LastExitCode请注意,一些外部命令(例如robocopy)使用退出代码不仅用于发送错误信号,还用于提供非错误状态信息。
https://stackoverflow.com/questions/41700013
复制相似问题