问题
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