Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 将powershell错误处理到批处理文件中_Windows_Powershell_Batch File_Error Handling - Fatal编程技术网

Windows 将powershell错误处理到批处理文件中

Windows 将powershell错误处理到批处理文件中,windows,powershell,batch-file,error-handling,Windows,Powershell,Batch File,Error Handling,我的powershell代码与此类似: If ($a -eq $false) { "FALSE" exit 5 } Else { If ($b -eq $false) { "FALSE" exit 5 } } 我已使用powershell-Command“&{}”命令将此代码导入到批处理文件中,我希望我的rest批处理代码仅在powershell命令未返回错误的情况下继续($a或$b为false) 到目前为止,我尝试了许多

我的powershell代码与此类似:

If ($a -eq $false)
{
    "FALSE"
    exit 5
}
Else
{
    If ($b -eq $false)
    {
        "FALSE"
        exit 5
    }
}
我已使用
powershell-Command“&{}”
命令将此代码导入到批处理文件中,我希望我的rest批处理代码仅在powershell命令未返回错误的情况下继续($a或$b为false)

到目前为止,我尝试了许多类似的方法,但它们立即关闭了我的批处理文件:

@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。
echo%errorlevel%
在这种情况下打印“5”,但似乎if语句不起作用。

在PowerShell中,退出时返回一个整数:

Exit 0

在批处理文件中,按如下方式运行PowerShell脚本:

powershell -Command "& {C:\scripts\myscript.ps1; exit $lastexitcode}"
现在,PowerShell会将退出代码从脚本返回到调用批处理文件,您可以在其中使用它:

echo %errorlevel%

在PowerShell中,退出时返回一个整数:

Exit 0

在批处理文件中,按如下方式运行PowerShell脚本:

powershell -Command "& {C:\scripts\myscript.ps1; exit $lastexitcode}"
现在,PowerShell会将退出代码从脚本返回到调用批处理文件,您可以在其中使用它:

echo %errorlevel%

我的
echo%errorlevel%
命令已经打印了errorlevel。另外,我没有使用单独的.ps1文件。我的
echo%errorlevel%
命令已经打印了errorlevel。另外,我没有使用单独的.ps1文件。在批处理中,块用括号括起来
()
而不是用括号括起来
{}
。此外,左括号必须位于块开始的行中,如ìf“%errorlevel%”eq“5”(`。参见示例答案。在批处理中,块被括在括号
()
中,而不是括在括号
{}
中。此外,左括号必须位于块开始的行中,如ìf“%errorlevel%”eq“5”(`。参见示例答案。谢谢你的回答。我最终用
eq
而不是
NEQ
做了一些类似于你的代码的事情。当我回答@elzooilogico的回答时,问题是一个非常简单的空格。我恐怕被这句话弄糊涂了。在你对elzooilogico的回答中,你说过“问题是我在5和(“)之间没有空格,但是如果你回顾你的问题,没有任何代码带有
5(
),因此问题不是一个非常简单的空格。我的回答显示了编写此代码的正确方法:在
if
的同一行中使用括号(而不是大括号)在
'5'
)之间有一个空格,所以我不明白你说的“经过多次测试,终于发现。。。".我完全理解你为什么会感到困惑。关于测试,我的意思是我已经测试过将大括号改为括号,并尝试了许多处理错误级别的方法,但几乎所有的测试都没有用,因为实际错误只是一个空格。此外,在阅读你的回复之前,我已经找到了解决方案,但我认为谢谢你的回答。我最终用
eq
而不是
NEQ
做了一些类似于你的代码的事情。当我回答@elzoioliogico的回答时,问题是一个非常简单的空格。我恐怕被这句话弄糊涂了。在你对elzoioliogico的回答中,你说:d“问题是我在5和(“)之间没有空格,但是如果你回顾你的问题,没有任何带有
5(
)的代码,因此问题不是一个非常简单的空格。我的回答显示了编写此代码的正确方法:在
if
的同一行中使用括号(而不是大括号)在
'5'
)之间有一个空格,所以我不明白你说的“经过多次测试,终于发现…”是什么意思.我完全理解你为什么会感到困惑。关于测试,我的意思是我已经测试过将大括号改为括号,并尝试了许多处理错误级别的方法,但几乎所有的测试都没有用,因为实际错误只是一个空格。此外,在阅读你的回复之前,我已经找到了解决方案,但我认为它是最好的,也更有用,所以我批准了它。