I have the following PowerShell function:
function Get-Devices {
[CmdletBinding()]
param(
[Parameter(Mandatory=$False, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True, ParameterSetName='Code')]
[String[]]$Codes
)
foreach ($Code in $Codes){write-output $Code}
}
While I call like this:
Get-Devices -Code 92D, 88L
It produced the following output:
92 88
Notice, the last character D and the character L is stripped off. I can get it to work if I enclose the values in quotes. Additionally, all of the below will all work as expected:
Get-Devices -Code D92, L88
Get-Devices -Code 92E, 88F
Get-Devices -Code 34G, 12Z
Get-Devices -Code "92D", "88L"
Especially note the 2nd line above. It's only the D
and L
suffices that have caused problems for me.
What's causing specifically D and L to be stripped off the end of each value? I really don't want to force people to add surrounding quotes to ALL values just to avoid this specific issue. What's the least complex way to force Powershell to not strip off D
or L
from the end of each value, without requiring surrounding quotes for those values?
I already tried changing the input Parameter type to: [Array[]]$Codes
. That didn't make any difference in behavior. I also confirmed all values are treated as a string type when processed by the above loop. Any help to avoid this behavior would be greatly appreciated!
The only good news is that this little piece of code and respective strange behavior is easily reproducible....