我有类似的shellhell代码:
If ($a -eq $false)
{
"FALSE"
exit 5
}
Else
{
If ($b -eq $false)
{
"FALSE"
exit 5
}
}
我已使用powershell -Command "& { }"
命令将此代码导入批处理文件中,并且我希望我的其他批处理代码仅在powershell命令未返回错误的情况下继续($ a或$ b为假)。
到目前为止,我尝试过很多这样的事情,但他们会立即关闭我的批处理文件:
@echo off
`some batch code here`
powershell -Command "& { }"
echo %errorlevel%
if '%errorlevel%' NEQ '5'
{
`code here`
}
echo ERROR
pause
exit
我这样做的最好方法是,如果我可以退出powershell的“if”块,例如代码为“5”,并在批处理中放入一个if语句来检查错误是否为5. {{1}在这种情况下打印“5”但似乎if语句不起作用。
答案 0 :(得分:1)
@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
答案 1 :(得分:0)
在PowerShell中,退出时返回一个Integer:
Exit 0
或
Exit 25
批处理文件中的运行PowerShell脚本,如下所示:
powershell -Command "& {C:\scripts\myscript.ps1; exit $lastexitcode}"
现在,PowerShell会将退出代码从脚本返回到调用批处理文件,您可以在其中使用它:
echo %errorlevel%