Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merge-Hashtables (Ordered)
function Merge-Hashtables([ScriptBlock]$Operator)
{
$Output = [ordered]@{}
ForEach ($Hashtable in $Input) {
If ($Hashtable -is [Hashtable])
{
ForEach ($Key in $Hashtable.Keys) {
$Output.$Key = If ($Output.ContainsKey($Key))
{
@($Output.$Key) + $Hashtable.$Key
}
Else
{
$Hashtable.$Key
}
}
}
elseif($Hashtable -is [System.Collections.Specialized.OrderedDictionary])
{
ForEach ($Key in $Hashtable.Keys) {
$Output.$Key = If ($Output.Contains($Key))
{
@($Output.$Key) + $Hashtable.$Key
}
Else
{
$Hashtable.$Key
}
}
}
}
If ($Operator)
{
ForEach ($Key in @($Output.Keys)) {
$_ = @($Output.$Key); $Output.$Key = Invoke-Command $Operator
}
}
$Output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.