Run script from command line and pass String array

So basically I need to run the ps1 script from the command line and pass my custom arguments to my script. My script is expecting a String array, but when I run the command, I get an error that the positional parameter cannot be found that takes the argument 'X1'

This is my command line:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy RemoteSigned -NoProfile -NonInteractive -File "C:\Program Files\MSBuild\MyScript.ps1" -builds "X1" "X2" "X3" "X4"

      

As I understand it, it knows what to do with the first parameter "X1" but not the second, and so it crashes? Any ideas?

+3


source to share


2 answers


You need to use a parameter -Command

instead of a parameter -File

. Notice the change in behavior in the screenshot below and the sample script.

[CmdletBinding()]
param (
    [int[]] $MyInts
)

foreach ($MyInt in $MyInts) {
    $MyInt + 1;
}

      



enter image description here

+2


source


I can't explain why it -file

doesn't work, but this setting has other known issues. When you use it, you are not getting the correct PowerShell exit code. Usage -command

works:



Powershell.exe -ExecutionPolicy RemoteSigned -NoProfile -NonInteractive -Command "& {& 'C:\Program Files\MSBuild\myscript.ps1' -builds x1,x2,x3}"

      

+2


source







All Articles