Skip to content

Commit

Permalink
ConvertTo-HashTable to support arrays (#3165)
Browse files Browse the repository at this point in the history
Co-authored-by: freddydk <[email protected]>
  • Loading branch information
freddydk and freddydk authored Aug 24, 2023
1 parent 5e41b84 commit 32c1f54
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions Common/ConvertTo-HashTable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,45 @@ function ConvertTo-HashTable() {
$object,
[switch] $recurse
)

function AddValueToHashTable {
Param(
[hashtable] $ht,
[string] $name,
$value,
[switch] $recurse
)

if ($ht.Contains($name)) {
throw "Duplicate key $name"
}
if ($recurse -and ($value -is [System.Collections.Specialized.OrderedDictionary] -or $value -is [hashtable] -or $value -is [System.Management.Automation.PSCustomObject])) {
$ht[$name] = ConvertTo-HashTable $value -recurse
}
elseif ($recurse -and $value -is [array]) {
$ht[$name] = @($value | ForEach-Object {
if (($_ -is [System.Collections.Specialized.OrderedDictionary]) -or ($_ -is [hashtable]) -or ($_ -is [System.Management.Automation.PSCustomObject])) {
ConvertTo-HashTable $_ -recurse
}
else {
$_
}
})
}
else {
$ht[$name] = $value
}
}

$ht = @{}
if ($object -is [System.Collections.Specialized.OrderedDictionary] -or $object -is [hashtable]) {
$object.Keys | ForEach-Object {
if ($recurse -and ($object."$_" -is [System.Collections.Specialized.OrderedDictionary] -or $object."$_" -is [hashtable] -or $object."$_" -is [PSCustomObject])) {
$ht[$_] = ConvertTo-HashTable $object."$_" -recurse
}
else {
$ht[$_] = $object."$_"
}
AddValueToHashTable -ht $ht -name $_ -value $object."$_" -recurse:$recurse
}
}
elseif ($object -is [PSCustomObject]) {
elseif ($object -is [System.Management.Automation.PSCustomObject]) {
$object.PSObject.Properties | ForEach-Object {
if ($recurse -and ($_.Value -is [System.Collections.Specialized.OrderedDictionary] -or $_.Value -is [hashtable] -or $_.Value -is [PSCustomObject])) {
$ht[$_.Name] = ConvertTo-HashTable $_.Value -recurse
}
else {
$ht[$_.Name] = $_.Value
}
AddValueToHashTable -ht $ht -name $_.Name -value $_.Value -recurse:$recurse
}
}
$ht
Expand Down

0 comments on commit 32c1f54

Please sign in to comment.