I have been adding $psBoundParameters to a hash table like this.
$seedTaskState = @{
arguments = $PSBoundParameters
}
Later, I need to use those same arguments to call the next function, with one of the values changed. So I used this
$nestedTaskParameters = $seedTaskState.arguments
and then changed the one value I needed to change. Which... doesn't work because complex types are by reference so I was changing the original bound parameters, which causes all sorts of issues.
I can just initialize $nestedTaskParameters
as a hash table and loop through the bound parameters and add them, like so.
$nestedTaskParameters = @{}
foreach ($key in $seedTaskState.arguments.Keys) {
$nestedTaskParameters.Add($key, $seedTaskState.arguments.$key)
}
And I had thought that this might work, and be more elegant
$nestedTaskParameters = $seedTaskState.arguments.Clone()
but .Clone()
is only available with a hashtable, not a bound parameters dictionary.
So I tried casting $seedTaskState.arguments
to a hash table first, then cloning, like this.
$nestedTaskParameters = ([Hashtable]$seedTaskState.arguments).Clone()
That seems to work, but is also well outside my comfort zone, so I wonder if there is some sort of gotcha with this approach?