如何将数组作为参数传递给另一个脚本? [英] How do I pass an array as a parameter to another script?

查看:27
本文介绍了如何将数组作为参数传递给另一个脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我似乎无法将字符串数组作为参数传递给脚本块.我在这里做错了什么?

For some reason, it looks like I cannot pass array of strings as parameter to scriptblock. What am I doing here wrong?

从另一个脚本调用的我的脚本:

My script which is called from another script:

param(
    [parameter(Mandatory=$true)]
    [string[]]$myarr
)

foreach ($elem in $myarr){
    $elem
}

我从另一个脚本调用它

 $myarr = @("111", "222")
 start-job -filepath myscript.ps1 -arg $myarr

我只得到了数组中的第一项 - 111".

I got only the first item in the array - "111".

推荐答案

尝试如下:

start-job -filepath myscript.ps1 -arg (,$myarr)

-ArgumentList 接受一个参数列表/数组.因此,当您给出 -arg $myarr 时,就好像您将数组的元素作为参数传递一样.因此,您必须强制 PowerShell 将其视为一个数组参数.

The -ArgumentList takes in a list/array of arguments. So when you give -arg $myarr, it is as though you are passing the elements of the array as the arguments. So you have to force PowerShell to treat it as a single argument which is an array.

这篇关于如何将数组作为参数传递给另一个脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆