Passing integer array as command line argument to powershell script

Viewed 4202

I have the following powershell script:

param (
  [Parameter(Mandatory=$true)][int[]]$Ports
)

Write-Host $Ports.count

foreach($port in $Ports) {
 Write-Host `n$port
}

When I run the script with $ powershell -File ./test1.ps1 -Ports 1,2,3,4 it works (but not as expected):

1

1234

When I try to use larger numbers, $ powershell -File .\test.ps1 -Ports 1,2,3,4,5,6,10,11,12, the script breaks entirely:

test.ps1 : Cannot process argument transformation on parameter 'Ports'. Cannot convert value "1,2,3,4,5,6,10,11,12" to type "System.Int32[]". Error: "Cannot convert value "1,2,3,4,5,6,10,11,12" to type "System.Int32". Error: "Input
string was not in a correct format.""
    + CategoryInfo          : InvalidData: (:) [test.ps1], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,test.ps1

It seems like powershell is trying to process any numbers passed via the Ports param as a single number, though I'm not sure why this is happening, or how to get past it.

1 Answers
Related