Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass array of arguments to Powershell commandline

Tags:

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?

like image 596
Samselvaprabu Avatar asked Nov 07 '12 06:11

Samselvaprabu


People also ask

How do I pass multiple command line arguments in PowerShell?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

How do I run a PowerShell script from the command line with parameters?

You can run scripts with parameters in any context by simply specifying them while running the PowerShell executable like powershell.exe -Parameter 'Foo' -Parameter2 'Bar' . Once you open cmd.exe, you can execute a PowerShell script like below.

How do I add to an array in PowerShell?

To add an item to an array, we can have PowerShell list all items in the array by just typing out its variable, then including + <AnotherItemName> behind it.

What is @() in PowerShell?

What is @() in PowerShell Script? In PowerShell, the array subexpression operator “@()” is used to create an array. To do that, the array sub-expression operator takes the statements within the parentheses and produces the array of objects depending upon the statements specified in it.


2 Answers

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

like image 99
Murphy Kuo Avatar answered Oct 20 '22 09:10

Murphy Kuo


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
like image 23
CB. Avatar answered Oct 20 '22 07:10

CB.