我有类似于以下内容的powershell代码:
If ($a -eq $false)
{
"FALSE"
exit 5
}
Else
{
If ($b -eq $false)
{
"FALSE"
exit 5
}
}
我已使用
powershell -Command "& { }"
命令将此代码导入到批处理文件中,并且我希望其余的批处理代码仅在powershell命令未返回错误($ a或$ b为false)的情况下才继续。到目前为止,我尝试了很多类似的操作,但是它们立即关闭了我的批处理文件:
@echo off
`some batch code here`
powershell -Command "& { }"
echo %errorlevel%
if '%errorlevel%' NEQ '5'
{
`code here`
}
echo ERROR
pause
exit
对我而言,最好的方法是,例如,可以退出代码为“5”的powershell的“if”块,然后分批放置if语句以检查错误是否为5。
echo %errorlevel%
在其中打印“5”情况下,但似乎if语句不起作用。
最佳答案
@echo off
`some batch code here`
powershell -Command "& { if $a -eq $false -or $b -eq $false { 'FALSE'; exit 5} }"
echo %errorlevel%
if '%errorlevel%' NEQ '5' (
`code here`
)
echo ERROR
pause
exit
关于windows - 将Powershell错误处理到批处理文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39674280/