Your code alone doesn't explain the symptom, but your own answer hints at an interesting pitfall that is worth exploring:
By default, a PowerShell variable can accept a value of any type, and values of different types can be assigned at any time:
$var = 42 # $var now contains an [int]
# ...
$var = 'hi' # $var now contains a [string]
However, you can type-constrain variables, in which case they only ever accept values of that type - or values that can automatically be converted to that type (PowerShell is very flexible when it comes to automatic conversions):
# Create a type-constrained variable.
[int] $var = 42 # $var now contains an [int] AND is locked into storing [int]s
# Try to assign a [string] that CANNOT be converted to an [int]
$var = 'hi' # FAILS: 'Cannot convert value "hi" to type "System.Int32"...'
# Try to assign a [string] that CAN be converted to an [int]
$var = ' 42 ' # OK - string was converted; $var now contains [int] 42
Typically, but not necessarily, parameter variables are type-constrained, as part of a script or function's list of declared parameters.
If you later reuse a type-constrained parameter variable by assigning a new value to it, it will enforce its original type, as described above.
The likeliest explanation in your case is that your $destinationInterclusterLifIps
was declared as a type-constrained [string] $destinationInterclusterLifIps
parameter, in which case a later attempt to assign an array resulted in implicit stringification of that array; to illustrate with a simple example:
function foo {
param(
# Type-constrained parameter.
[string] $destinationInterclusterLifIps
)
# ...
# Try to assign an *array*:
# Instead of storing an array, the [string] type constraint
# converts the array to a *single string*, as a space-separated list of
# its elements.
$destinationInterclusterLifIps = 1, 2, 3
# Output the value.
$destinationInterclusterLifIps
}
# Call the function (no argument needed)
foo
This outputs:
1 2 3
i.e., the array converted to a string, as a space-separated list of its elements.
(An array would print element by element, each on its own line).
Note that your problem was therefore unrelated to the use of a hash table and its conversion to JSON - it's just where you happened to notice the problem.
Optional reading: Modifying / removing a variable's type constraint:
It is possible to later recreate a type-constrained variable with a different type; e.g.:
[int] $var = 42; [string] $var = 'hi'
While you can use Remove-Variable
to effectively remove a type constraint by removing the variable first and then recreating it unconstrained, as Maximilian Burszley suggests -
[int] $var = 42; Remove-Variable var; $var = 'hi'
- you can use [object] $var = 'hi'
as a shortcut, since type-constraining to [object]
is tantamount to not constraining the value.
(Get-Variable var).Attributes
contains a variable's attributes, and a type-constraining attribute is stored as a [System.Management.Automation.ArgumentTypeConverterAttribute]
instance. If you're not concerned about removing all attributes, another simple, though obscure, option for removing a type constraint is to use (Get-Variable var).Attributes.Clear()
.