Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Script Arguments Passed as Array

Tags:

powershell

EDIT: I've changed the code here to a simple test case, rather than the full implementation where this problem is arising.

I am trying to call one Powershell script from another, but things aren't working out as I'm expecting. As I understand things, the "&" operator is supposed to expand arrays into distinct parameters. That's not happening for me.

caller.ps1

$scriptfile = ".\callee.ps1"
$scriptargs = @(
    "a",
    "b",
    "c"
)

& $scriptfile $scriptargs

callee.ps1

Param (
    [string]$one,
    [string]$two,
    [string]$three
)

"Parameter one:   $one"
"Parameter two:   $two"
"Parameter three: $three"

Running .\caller.ps1 results in the following output:

Parameter one:   a b c
Parameter two:
Parameter three:

I think that the problem I'm experiencing is $scriptargs array is not expanded, and is rather passed as a parameter. I'm using PowerShell 2.

How can I get caller.ps1 to run callee.ps1 with an array of arguments?

like image 520
Sam Avatar asked Oct 24 '13 18:10

Sam


People also ask

How do I pass multiple arguments in PowerShell script?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

How do you pass an array in PowerShell?

$myArray = @('Here', 'are', 'some', 'things') $myArray | ForEach-Object ` -Begin { Write-Host "Start!" } ` -Process { Write-Host $_ } ` -End {Write-Host "Finished!"}

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 does @() mean in PowerShell?

Array subexpression operator @( )Returns the result of one or more statements as an array. The result is always an array of 0 or more objects. PowerShell Copy.


2 Answers

When invoking a native command, a call like & $program $programargs will correctly escape the array of arguments so that it is parsed correctly by the executable. However, for a PowerShell cmdlet, script, or function, there is no external programming requiring a serialize/parse round-trip, so the array is passed as-is as a single value.

Instead, you can use splatting to pass the elements of an array (or hashtable) to a script:

& $scriptfile @scriptargs

The @ in & $scriptfile @scriptargs causes the values in $scriptargs to be applied to the parameters of the script.

like image 176
Emperor XLII Avatar answered Oct 01 '22 08:10

Emperor XLII


You're passing the variables as a single object, you need ot pass them independently.

This here works:

$scriptfile = ".\callee.ps1"
& $scriptfile a b c

So does this:

$scriptfile = ".\callee.ps1"
$scriptargs = @(
    "a",
    "b",
    "c"
)

& $scriptfile $scriptargs[0] $scriptargs[1] $scriptargs[2]

If you need to pass it as a single object, like an array, then you can have the callee script split it; the specific code for that would depend on the type of data you're passing.

like image 21
cyiton Avatar answered Oct 01 '22 08:10

cyiton