如何将数组作为参数传递给另一个脚本?
问题描述:
由于某种原因,看来我无法将字符串数组作为参数传递给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.