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

标签 powershell batch-file

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

相关文章:

azure - 如何将 azure VM 镜像移动到其他位置

powershell - 通过电子邮件发送 Powershell 脚本的所有输出

powershell - 仅使用 powershell,对包含 300 行的文本文件进行排序,首先按字符串长度排序,然后,一旦达到该长度,按字母顺序设置它们

windows - 有什么办法可以使用Powershell在文件夹中找到 'metadata'

Windows x64 & "parenthesis in path"批处理文件问题

for-loop - Windows批处理中以逗号分隔的for循环列表

windows - 在 Windows 批处理文件的 FOR 循环中转义星号

windows - 使用批处理脚本自动将最新文件复制到文件夹

unicode - 如果调用 chcp,则不会执行批处理脚本

powershell - 当其他用户使用PowerShell注销时自动登录用户