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

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

问题描述

由于某种原因,看来我无法将字符串数组作为参数传递给scriptblock.我在这里做什么错了?

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".

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