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

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

问题描述

由于某些原因,它看起来像我无法通过字符串作为参数传递给脚本块的数组。我在做什么在这里错了?

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
}

我把它从另一个脚本

I call it from another script as

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

在阵列中只拿到了第一个项目 - 111

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天全站免登陆