如何将多个值从 powershell 脚本返回到调用它的批处理文件?
我有一个返回多个值的 powershell 脚本。我想从批处理文件中调用它,并将每个单独的值放入批处理文件中的单独变量中。
只能返回一个值
powershell代码(pstest.ps1):
$p1=11
$p2=22
$p3=33
exit
批处理文件:
powershell .\pstest.ps1
:: now I'd like to get those 3 returned values
:: into 3 individual variables so that I can do something like this:
@echo First is %p1%, Second is %p2%, Third is %p3%
所以,它应该显示:
First is 11, Second is 22, Third is 33
最佳答案
以完全相同的方式从任何应用程序获取多个值:每行一个...
@echo off
setlocal EnableDelayedExpansion
set "i=0"
for /F "delims=" %%a in ('powershell "$p1=11; $p2=22; $p3=33; $p1; $p2; $p3"') do (
set /A i+=1
set "p!i!=%%a"
)
echo First is %p1%, Second is %p2%, Third is %p3%
我建议您阅读this answer关于数组...
关于powershell - 如何将多个值从 powershell 脚本返回到调用它的批处理文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56570795/