如何将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
发布于 2019-06-14 00:00:41
以完全相同的方式从任何应用程序获得多个值:每行一个值……
@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 ...
发布于 2019-06-13 06:48:41
除了使用环境变量之外,您还可以尝试使用SachaDee的答案here进行如下操作
PS:
function get-multiplereturnvalues {
"11"
"22"
"33"
}
get-multiplereturnvalues
批处理/命令行
@echo First is %p1%, Second is %p2%, Third is %p3%
for /f "delims=" %%a in ('powershell .\multi.ps1') do @echo "$Value=%%a"
以下哪项输出:
"$Value=11"
"$Value=22"
"$Value=33"
发布于 2019-06-13 06:49:20
在PowerShell脚本中,我将创建一个包含结果的临时文件。在批处理文件中,在调用PowerShell脚本之后,我将使用/F解析该临时文件,以获得结果并根据需要设置环境变量。
https://stackoverflow.com/questions/56570795
复制相似问题