6

I am trying to pass an array to a PowerShell script, but I always get just one value. I have googled my butt off, but I can't find anything. All I need to do is pass an array to the script. Here is my code:

param($Location)
($location).count

Foreach ($loc in $Location)
{

$loc

}

Here is my command I am running:

C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -ExecutionPolicy Unrestricted  -File "C:\ParaTestingArray.ps1" -location Sydney,London

Here is the output:

1
Sydney

For the life of me I can't get it to grab the other value in the array. I have tried using

param([string[]]$Location)

I have tried:

-location "Sydney","London"
-location @(Sydney,London)
-location Sydney London
-location Sydney,London
-location (Sydney,London)

What am I doing wrong?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2142466
  • 105
  • 1
  • 1
  • 5
  • 1
    Are you passing from a PS script to a PS Script? – Austin T French Mar 07 '13 at 02:51
  • 1
    The problem is that the script is expecting an array, but when you run powershell.exe -file ... (etc.) the input string @('a','b','c') is not coerced into an array. If you run the script from a PowerShell prompt, the problem will not occur. – Bill_Stewart Mar 07 '13 at 03:23
  • 1
    AdqBill - I need the script to be in a schedule task with each task giving a different location. How would you run the scrip then with out using my method? – user2142466 Mar 07 '13 at 03:44
  • AthomSefre - Nope, just want to send an array to a PS script. – user2142466 Mar 07 '13 at 03:46

3 Answers3

8

Use the -command switch instead.

Like this:

powershell -Command "&{C:\ParaTestingArray.ps1 -Location A,B,C}"

From the about_powershell:

For writing a string that runs a Windows PowerShell command, use the format:

     "& {<command>}"

Where the quotation marks indicate a string and the invoke operator (&) causes the command to be executed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fridden
  • 189
  • 2
1

I cannot reproduce that result:

$script = @'
param($Location)
($location).count

Foreach ($loc in $Location)
{

$loc

}
'@

$script | sc test.ps1

.\test.ps1 sydney,london

2 sydney london

Edit: This works:

$args.count

Foreach ($loc in $args)
{

$loc

}

Called as: powershell.exe -file c:\test.ps1 sydney london

mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • Thanks mjolinor. The way you do it I get the same result as you. What is wrong with the way I am doing it? Can you try it my way and let me know what you get. Thanks agian!!! – user2142466 Mar 07 '13 at 02:59
  • I get the same result you do if I call it that way. Not sure how you'd do that. – mjolinor Mar 07 '13 at 03:46
  • 1
    I think the problem is that there's no way to present an array as a CMD string argument. You'll need to use a delimited string, and split it. – mjolinor Mar 07 '13 at 03:50
  • $args means "the arguments passed to the script or function." It can be iterated the same as an array. – Bill_Stewart Mar 07 '13 at 04:42
  • This technique does not work when you need to pass in the array using a named parameter, i.e. when both args populate the same parameter. WHat is being shown when iterating over the $args variable is actually iterating over two different arguments. The answer from Fridden is the correct way to pass an array in a sungle argument to a script using powershell.exe. – Orlando Colamatteo Jul 11 '13 at 20:44
0

The

Powershell.exe -Command "&{C:\script.ps1 -ParmArray 1,2,3}"

trick worked for me - but it feels unfortunately hacky. Definitely counter-intuitive after the rest of my powershell experience.

Serjik
  • 10,543
  • 8
  • 61
  • 70