简体   繁体   中英

Powershell - Pass an array as argument to an executable called from command line

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

在应用程序中调试信息 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?

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM