我正在尝试使用命令从批处理文件中执行 powershell:
Powershell .\namefile.ps1
PowerShell 返回一些值 1, 4, 0 和 -1 。如何从批处理中获取这些值?当我使用 %errorlevel% 时,它只返回 0(这意味着脚本没问题)。我也尝试过在 PowerShell (Exit 4) 中使用 Exit 命令,但它不起作用。
你能帮助我吗?
编辑
如果有人感兴趣,我已经找到了解决方案。
powershell "&{.\test.ps1 %* ;exit $LastExitCode}" set code=%errorlevel%
最佳答案
如果您需要在 bat
中使用此值环境使用FOR /F
:
@echo off
for /f "delims=" %%a in ('powershell .\test.ps1') do Set "$Value=%%a"
Echo Value received from Powershell : %$Value%
关于powershell - 如何从powershell获取返回值并将其放入批处理文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34451444/