2

PowerShell drops the trailing zero of an array element's value when that value contains a dot. Unless I wrap the value in quotes. Unfortunately I need to retain the trailing zero and the script users fail to use quotes reliably.

How can I coerce PowerShell to preserve the trailing zero?

  • The array element format is Number.Number
  • The array element may have 1 to n trailing zeros
  • Single element arrays retain trailing zeros
  • Multi element arrays drop trailing zeros

Strongly typing the parameter as [string[]] does not resolve the issue.

Example:

function Get-Customer {
    Param 
    ( 
        [string[]]$CustomerId
    )
    $CustomerId
}

> Get-Customer -CustomerId 654.1,654.10,654.1721,654.1720
654.1    #CORRECT
654.1    #INVALID
654.1721 #CORRECT
654.172  #INVALID

3 Answers 3

2

You cannot preserve that 0 if the caller does not put it in quotes. The fact is that those values will be interpreted as numbers because they fit the pattern of number literals.

So if you can't change your caller's behavior then they will be numbers before they ever enter your function. Your [string[]] cast will convert the number to a string, but it's already a number at that point and will follow number -> string rules.

PowerShell is very lenient when it comes to type conversion, or rather, it tries hard to successfully convert types when there is a mismatch, so it will be difficult to throw an error in this case too (you have no access to the original values to know anything is wrong as this happens during parameter binding).

You could do this:

function Get-Customer {
    Param 
    ( 
        [ValidateScript({
            $_ -is [string]
        })]
        [object[]]$CustomerId
    )
    $CustomerId
}

It would force the values passed in to already be a [string], which kind of sucks for all the other cases where string conversion would have been useful though.

1
  • Bummer. Well, thank you for the concise, helpful explanation. At least with your solution I could immediately alert the caller when quotes are not employed.
    – gstratt
    Commented Apr 13, 2019 at 12:53
1

I'm with briantist, to reduce quoting, you could split one string:

function Get-Customer {
    Param 
    ( 
        [ValidateScript({
            $_ -is [string]
        })]
        [object[]]$CustomerId
    )
    $CustomerId -split ','
}

> Get-Customer -CustomerId '654.1,654.10,654.1721,654.1720',"1.000,2.00"
654.1
654.10
654.1721
654.1720
1.000
2.00
1
  • Yes that could help lower the burden on my script caller. Thanks!
    – gstratt
    Commented Apr 13, 2019 at 13:35
1

How about this trick. No commas, no quotes, still getting an array and maintaining all your items as is, like this...

function Get-CustomerId 
{ $args }
New-Alias -Name cid -Value Get-CustomerId 
cid 654.1 654.10 654.1721 654.1720

# Results

654.1
654.10
654.1721
654.1720

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.