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?
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.
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.
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 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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With