如何从powershell获取返回值并将其放入批处理文件中?

Yam*_*Yam 5 powershell batch-file

我正在尝试使用commande:Powershell从批处理文件执行powershell.\nameoffile.ps1

powershell返回一些值1,4,0​​和-1.如何从批次中获取这些值?当我使用%errorlevel%时,它只返回0(这意味着脚本没问题).我也尝试在powershell中使用Exit命令(退出4),但它不起作用.你能帮助我吗?

问候

编辑 我找到了解决方案,如果有人有兴趣.谢谢你的时间

powershell"&{.\ test.ps1%*;退出$ LastExitCode}"set code =%errorlevel%

Sac*_*Dee 9

如果您需要在bat环境中使用此值,请使用bat:

@echo off
for /f "delims=" %%a in ('powershell .\test.ps1') do Set "$Value=%%a"

Echo Value received from Powershell : %$Value%
Run Code Online (Sandbox Code Playgroud)


Yam*_*Yam 6

powershell "&{.\test.ps1 %* ;exit $LastExitCode}" set code=%errorlevel%
Run Code Online (Sandbox Code Playgroud)