0

Wondering how I could pass an array as command line argument to an exe file in powershell? Here is what I am currently working on

I am calling an exe file from a powershell function in the following format

$firstParam = "test1"
$secondParam = "test2"

$thirdParam = @()
$thirdParam = 'test3'
$thirdParam = $thirdParam + 'test4'

[void](& '..\SomeApp.exe' "$firstParam" "$secondParam" "$thirdParam"

Here is what I am seeing as the input arguments in the Application.exe

Debugging information in the application The third input parameter that was passed from the powershell was an array but it got concatenated (space separated) when passed to the exe file.

Is it possible to pass "test3" as the third argument and "test4" as the fourth argument?

infinity
  • 1,900
  • 4
  • 29
  • 48
  • 1
    Note that putting all of your arguments in an array would also work; e.g. `$appArgs = @($firstParam,$secondParam); $appArgs += $thirdParam; $appArgs += $fourthParam; [void](& '..\SomeApp.exe' $appArgs)`. See also [§2.2](http://stackoverflow.com/a/8468690/2495). – Emperor XLII Oct 18 '12 at 13:39

2 Answers2

1

$thirdParam cannot be an array with your implementation. When you write $thirdParam = @(), you do declare an empty array, but then you re-assign it to a string: $thirdParam = 'test3' and then to another string $thirdParam = $thirdParam + 'test4'. I am still not clear about your original intent, but here's how you would pass test3 as third argument and test4 as the fourth:

$fourthParam = 'test4'
[void](& '..\SomeApp.exe' "$firstParam" "$secondParam" "$thirdParam" "$fourthParam"

If you only have 2 fixed parameters, and you can have N parameters, switch to Invoke-Expression instead of Invoke-Command:

[void](Invoke-Expression "..\SomeApp.exe $firstParam $secondParam $thirdParam"

and make sure your parameters are correctly quoted. In this case, if $thirdParam contains spaces, it will determine your parameter #4 and onwards.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • Hi Neolisk - Thanks for your inputs! The issue I have is that I only know that the first two parameters exist before hand and third parameter onward is dynamic. I need to accomodate the following scenarios Parameters to pass - test1, test2, test3, test4 - test1, test2, test3, test4, test5 – infinity Oct 18 '12 at 00:20
1

I found this question while using Google when I was asking myself the same thing and want to point out that Windows PowerShell 3.0, which was released in October 2012, and later offers a way to do so via splatting.

In OP's example, the code would look as follows as he is passing positional parameters (at least from a PowerShell point of view) when invoking an executable via the call operator:

$firstParam = 'test1'
$secondParam = 'test2'

$thirdAndFourthParam = , 'test3'
$thirdAndFourthParam += 'test4'

[void](& '..\SomeApp.exe' $firstParam $secondParam @thirdAndFourthParam)

When named parameters need to be passed to a PowerShell command, a hash table of the form

$Params = @{ NamedParam = $Value }

needs to be used instead of an array. If NamedParam is a switch, then $Value can either be $True or $False.

(That being said, Victor is of course correct in pointing out in his answer that $thirdParam in OP's example is not an array but a string that is being appended.)

da22e
  • 38
  • 4