如何从Powershell获取退出代码并返回到CMD? [英] How to get exitcode from Powershell and return to CMD?
本文介绍了如何从Powershell获取退出代码并返回到CMD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个powershell脚本.我使用CMD文件运行powershell脚本.我想从我的Powershell脚本中获取退出代码,然后将该值返回给CMD. 我试过了但是当我执行CMD文件调用Powershell时,它不会返回退出代码.
I have a powershell script. I run my powershell script using a CMD file. I want to get the exit code from my powershell script and return the value to the CMD. I tried this. but it does not return the exitcode, when I execute the CMD file to call the powershell.
$SN = "17A1"
$BID = "#SBCM#DBCM"
$FB = "UdpdqfP.Bd"
$SN2 = Get-Content .\out4 | Where-Object{$_.Contains("$SN")}
if($SN2)
{
Write-Host "OK"
}
else{
Write-Host "Not ok"
$ExitCode = "ExitCode"
$ExitCode = "123"
Exit $ExitCode
}
CMD调用Powershell并返回退出代码
powershell.exe -ExecutionPolicy Bypass -File %~dp0\test.ps1
ECHO %ExitCode%
Exit /b %ExitCode%
我执行CMD文件,并返回此文件:
I execute the CMD file, and return this:
D:\XX\>powershell.exe -ExecutionPolicy Bypass -File D:\XX\\test.ps1
Not ok
D:\XX\>ECHO
ECHO is on.
D:\XX\>Exit /b
执行CMD文件后的期望:
My expectation once I execute the CMD file:
D:\Boot_Order>powershell.exe -ExecutionPolicy Bypass -File D:\Boot_Order\\test.ps1
Not ok
D:\Boot_Order>ECHO
123
D:\Boot_Order>Exit /b 123
推荐答案
批处理文件与环境变量%errorlevel%
一起使用,因此请将批处理文件更改为:
Batch files work with the environment variable %errorlevel%
so change the batch file to:
powershell.exe -ExecutionPolicy Bypass -File "%~dp0\test.ps1"
echo %errorlevel%
exit /b %errorlevel%
这篇关于如何从Powershell获取退出代码并返回到CMD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文