gpt4 book ai didi

powershell - 如何将多个值从Powershell脚本返回到正在调用它的批处理文件?

转载 作者:行者123 更新时间:2023-12-03 01:00:55 25 4
gpt4 key购买 nike

如何将多个值从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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com