How to pass array of arguments to Powershell commandline

流过昼夜 提交于 2020-01-01 04:16:14

问题


I am trying to pass array of arguments to powershell script file.

I was trying to pass the commandline like this in command line.

Powershell -file "InvokeBuildscript.ps1" "z:\" "Component1","component2"

But it doesn't take the parameters it seems. What am i missing? how to pass array of arguments?


回答1:


Short answer: More double quotes could help...

suppose the script is "test.ps1"

param(

    [Parameter(Mandatory=$False)]
    [string[]] $input_values=@()

)
$PSBoundParameters

Suppose would like to pass the array @(123,"abc","x,y,z")

Under Powershell console, to pass multiple values as an array

.\test.ps1 -input_values 123,abc,"x,y,z"

Under Windows Command Prompt Console or for Windows Task Scheduler; A double-quote are replaced by 3 double-quotes

powershell.exe -Command .\test.ps1 -input_values 123,abc,"""x,y,z"""

Hope it could help some




回答2:


try

Powershell -command "c:\pathtoscript\InvokeBuildscript.ps1" "z:\" "Component1,component2"

if test.ps1 is:

$args[0].GetType()
$args[1].gettype()

call it from a dos shell like:

C:\>powershell -noprofile -command  "c:\script\test.ps1" "z:" "a,b"

returns :

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
True     True     Object[]                                 System.Array


来源:https://stackoverflow.com/questions/13264369/how-to-pass-array-of-arguments-to-powershell-commandline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!