Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass array to PowerShell script executed with -File argument

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?

like image 661
Krzysztof Avatar asked Nov 27 '14 15:11

Krzysztof


People also ask

Can we pass an array as an argument to a function?

A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name. In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun().

How do you pass arguments to a PowerShell script?

Passing arguments in PowerShell is the same as in any other shell: you just type the command name, and then each argument, separated by spaces. If you need to specify the parameter name, you prefix it with a dash like -Name and then after a space (or a colon), the value.

What actually gets pass when you pass an array to a function argument?

In case, when an array (variable) is passed as a function argument, it passes the base address of the array. The base address is the location of the first element of the array in the memory.

How do I push an array in PowerShell?

You can use the += operator to add an element to an array. The following example shows how to add an element to the $a array. When you use the += operator, PowerShell actually creates a new array with the values of the original array and the added value.


1 Answers

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
like image 119
Micky Balladelli Avatar answered Oct 18 '22 20:10

Micky Balladelli