Passing an array to a PowerShell script

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

param($Location) ($location).count Foreach ($loc in $Location) { $loc } 

Here is my command that I run:

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

Here is the result:

1
Sydney

Throughout life, I cannot get it to capture another value in the array. I tried using

 param([string[]]$Location) 

I tried:

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

What am I doing wrong?

+4
source share
3 answers

I can not reproduce this 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

+1
source

Use -command .

Like this:

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

From about_powershell:

To write the line that runs the Windows PowerShell command, use the format:

  "& {<command>}" 

If quotation marks indicate a string and invoke operator ( & ) invokes the execution of the command.

+7
source

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

the trick worked for me - but unfortunately it was hacked. Definitely counter-intuitive after the rest of the experience of my strength.

0
source

All Articles