开发者

How do I pass an array as a parameter to another script?

开发者 https://www.devze.com 2023-03-29 17:52 出处:网络
For some reason, it looks like I cannot pass array of strings as parameter to scriptblock. What am I doing here wrong?

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

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


Try it like below:

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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消