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

For some reason, it seems like I cannot pass an array of strings as a parameter to a script block. What am I doing wrong here?

My script that is being 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 element in the array - "111".

+51
arrays parameters powershell arguments
Aug 22 '11 at 19:45
source share
1 answer

Try the following:

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

-ArgumentList accepts a list / array of arguments. Therefore, when you give -arg $myarr , you kind of pass the elements of the array as arguments. Therefore, you need to force PowerShell to treat it as the only argument, which is an array.

+69
Aug 22 '11 at 20:08
source share



All Articles