4

So basically I need to run my ps1 script from command line and pass my custom arguments into my script. My script expects a String array, but when I run the command, I get an error that a positional parameter cannot be found that accepts 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"

My understanding is it knows what to do with the first parameter 'X1' but not the second and so it crashes? Any ideas whay?

2
  • I'm not familar with powershell and I don't know your script -properbly you should post it here- must not the elements seperated with colon, like "X1,X2,X3,.."? Commented Aug 29, 2014 at 14:16
  • doesn't make a difference with the comma or not Commented Aug 29, 2014 at 14:21

2 Answers 2

3

You need to use the -Command parameter instead of the -File parameter. Notice the change in behavior from the screenshot below, and the sample script.

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

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

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I should have known. I gotta figure something out since a RunScript in TFS 2013 uses the -File and not -Command. Ill need to modify my ps1 script to recognize the commas and create the string array in there.
2

I can't quite explain why -file is not working but that parameter has other known issues. When you use it you don't get a proper exit code from PowerShell. Using -command does work:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.