9

I have simple script which takes array argument:

param(
    [Parameter(Mandatory=$true)]
    [string[]]$keys
)

for ($i = 0; $i -lt $keys.Length; ++$i) {
    Write-Host "$i. $($keys[$i])"
}

I need to execute it via powershell with -File argument (in order to address a TeamCity bug) like so:

powershell.exe -File Untitled2.ps1 -keys a

How can I pass parameter as array to my script? As long as I pass single key it works well, but it don't want to take more than one element.

I tried following among others:

powershell.exe -File Untitled2.ps1 -keys a,b
powershell.exe -File Untitled2.ps1 -keys:a,b
powershell.exe -File Untitled2.ps1 -keys $keys # where $keys is an array

Whatever I'd tried either I have "A positional parameter cannot be found" error, or all keys are joined in first array element.

Any ideas?

5
  • have you tried "a","b" or @("a","b")? or start-process with -argumentlist? Commented Nov 27, 2014 at 15:20
  • Yes, and few others with same result Commented Nov 27, 2014 at 15:21
  • I need to use -file, because it is executed from teamcity, and it has bug which causes infinite loop in some cases when -Command is used. Commented Nov 27, 2014 at 15:24
  • This bypasses the requirment but could you pass the array as a string then cast it in the script instead with a Split for example? Commented Nov 27, 2014 at 16:04
  • Casting won't work, but it is quite obvious that I can pass param as csv for example an process it, but since ps supports array params, it should work for -File too. If it is not supported then I will be forced to do it differently, but for now nobody stated that this isn't supported. Commented Nov 27, 2014 at 16:30

1 Answer 1

9

Here is another try. Note the ValueFromRemainingArguments=$true in the parameters declaration:

param([parameter(Mandatory=$true,ValueFromRemainingArguments=$true)]
    [string[]]$keys)
for ($i = 0; $i -lt $keys.Length; ++$i) {
    Write-Host "$i. $($keys[$i])"
}

Then I called the script via powershell.exe using the -file argument:

powershell.exe -File d:\scripts\array.ps1 "1" "a" "c"

This works to pass all those parameters as an array, the output is:

0. 1
1. a
2. c

In case you need to pass additional parameters you can name them in the usual way, such as:

param([parameter(Mandatory=$true,ValueFromRemainingArguments=$true)]
    [string[]]$keys,
    [string] $dummy)

And you can pass the additional parameters such as this:

powershell.exe -File d:\scripts\array.ps1 "1" "a" "c" -dummy "Z"

The $dummy parameter will in this case receive the value Z while the values of "1" "a" "c" will still be assigned to $keys as an array.

So if change the script to display the value of $dummy along with the rest I get:

0. 1
1. a
2. c
Dummy param is z
Sign up to request clarification or add additional context in comments.

3 Comments

I know that this works, but it don't answer my question.
Modified answer to use -File.
As long as there is one array parameter in script it does the job.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.