简体   繁体   English

Powershell-将数组作为参数传递给从命令行调用的可执行文件

[英]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? 想知道如何将数组作为命令行参数传递给Powershell中的exe文件? Here is what I am currently working on 这是我目前正在从事的工作

I am calling an exe file from a powershell function in the following format 我正在从Powershell函数中以以下格式调用exe文件

$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 这是我在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. 从powershell传递的第三个输入参数是一个数组,但在传递给exe文件时被串联(用空格隔开)。

Is it possible to pass "test3" as the third argument and "test4" as the fourth argument? 是否可以将“ test3”作为第三个参数传递,并将“ test4”作为第四个参数传递?

$thirdParam cannot be an array with your implementation. $thirdParam不能是实现的数组。 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' . 当您编写$thirdParam = @() ,您确实声明了一个空数组,但随后将其重新分配给字符串: $thirdParam = 'test3' ,然后再分配给另一个字符串$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: 我仍然不清楚您的初衷,但是这是如何将test3作为第三个参数,并将test4作为第四个参数:

$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 : 如果只有2个固定参数,并且可以有N个参数,请切换到Invoke-Expression而不是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. 在这种情况下,如果$ thirdParam包含空格,它将确定参数#4及以后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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