[英]“(. was unexpected at this time” when running batch file from Powershell
[英]Unexpected exit code when running a batch file from PowerShell
我在 Microsoft Windows 环境中从Powershell
调用.bat
文件,并且想知道退出代码是成功还是失败。
在这种不寻常的(尽管不是很不寻常)边缘情况下似乎出现了问题。
示例 #1:
.\test1.bat
ECHO ON
setlocal enabledelayedexpansion
False
if !errorlevel! neq 0 exit /b !errorlevel!
echo "More text"
这工作正常。 False
导致错误; 下一行检查状态,看到1
并以代码1
exit
s。 调用powershell
有正确的结果。 echo $LASTEXITCODE
是1
。
示例 #2:
.\test2.bat
ECHO ON
setlocal enabledelayedexpansion
if "testing" == "testing" (
False
if !errorlevel! neq 0 exit /b !errorlevel!
echo "More text"
echo "More code"
True
if !errorlevel! neq 0 exit /b !errorlevel!
echo "More code"
)
这就是困难所在。 False
导致错误; 下一行检查状态,看到1
并以代码1
退出。 然后,调用powershell
出现意外结果, echo $LASTEXITCODE
is 0
。
只能修改批处理文件来解决问题吗?
You're seeing problematic cmd.exe
behavior when batch files are called from outside cmd.exe
, such as from PowerShell, their exit codes aren't reliably relayed as cmd.exe
's process exit code .
有关背景信息,请参阅此答案。
解决方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.