I ran into a snag when I passed a hash table by reference to a function for splatting purposes. How can I fix this?
Function AllMyChildren {
param (
[ref]$ReferenceToHash
}
get-childitem @ReferenceToHash.Value
# etc.etc.
}
$MyHash = @{
'path' = '*'
'include' = '*.ps1'
'name' = $null
}
AllMyChildren ([ref]$MyHash)
Result: an error (“Splatted variables cannot be used as part of a property or array expression. Assign the result of the expression to a temporary variable then splat the temporary variable instead.”).
Tried to do this:
$newVariable = $ReferenceToHash.Value
get-childitem @NewVariable
That did work and seemed right per the error message. Is it the preferred syntax in a case like this?
1) Passing hashtables (or any instances of classes, i.e. reference types) with
[ref]makes no sense because they are always passed by reference themselves.[ref]is used with value types (scalars and instances of structures).2) The splatting operator can be applied to a variable directly, not an expression.
Thus, in order to resolve the problem simply pass the hashtable in the function as it is: