4

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.

3
  • This is likely due to parsing by whatever is parsing your powershell command line. If you run your script (or function) from the PowerShell prompt itself, it should work as expected. Commented Aug 2, 2017 at 16:48
  • 2
    If I remember correctly, -file doesn't like argument arrays. -command works better. Does powershell -Command "& .\test.ps1 -Ports 1,2,3,4,5,6,10,11,12" work the way you want? Commented Aug 2, 2017 at 17:00
  • Seems to be runnning PoSh V 6.0.0beta on Linux/OSX @BenHs tip is working there fine. Commented Aug 2, 2017 at 17:20

1 Answer 1

6

The issue is that a parameter passed through powershell.exe -File is a [string].

So for your first example,

powershell -File ./test1.ps1 -Ports 1,2,3,4

$Ports is passed as [string]'1,2,3,4' which then attempts to get cast to [int[]]. You can see what happens with:

[int[]]'1,2,3,4'
1234

Knowing that it will be an just a regular [int32] with the comma's removed means that casting 1,2,3,4,5,6,10,11,12 will be too large for [int32] which causes your error.

[int[]]'123456101112'

Cannot convert value "123456101112" to type "System.Int32[]". Error: "Cannot convert value "123456101112" to type "System.Int32". Error: "Value was either too large or too small for an Int32.""


To continue using -file you could parse the string yourself by splitting on commas.

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

$PortIntArray = [int[]]($Ports -split ',')

$PortIntArray.count    

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

But luckily that is unnecessary because there is also powershell.exe -command. You can call the script and use the PowerShell engine to parse the arguments. This would correctly see the Port parameter as an array.

powershell -Command "& .\test.ps1 -Ports 1,2,3,4,5,6,10,11,12"
Sign up to request clarification or add additional context in comments.

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.