| | | 1 | | # Portions derived from PowerShell-Yaml (https://github.com/cloudbase/powershell-yaml) |
| | | 2 | | # Copyright (c) 2016–2024 Cloudbase Solutions Srl |
| | | 3 | | # Licensed under the Apache License, Version 2.0 (Apache-2.0). |
| | | 4 | | # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| | | 5 | | # Modifications Copyright (c) 2025 Kestrun Contributors |
| | | 6 | | |
| | | 7 | | <# |
| | | 8 | | .SYNOPSIS |
| | | 9 | | Convert a hashtable to an OrderedDictionary, converting any nested PSObjects to generic objects. |
| | | 10 | | .DESCRIPTION |
| | | 11 | | This function takes a hashtable as input and converts it to an OrderedDictionary. It ensures that any nested PSObjec |
| | | 12 | | The order of keys in the hashtable is preserved in the resulting OrderedDictionary. |
| | | 13 | | .PARAMETER Data |
| | | 14 | | The hashtable to convert. |
| | | 15 | | .EXAMPLE |
| | | 16 | | $ht = @{ "Key1" = "Value1"; "Key2" = [PSCustomObject]@{ Prop1 = "Val1"; Prop2 = "Val2" } } |
| | | 17 | | $dict = Convert-HashtableToDictionary -Data $ht |
| | | 18 | | # $dict is now an OrderedDictionary with Key1 and Key2, where Key2's value is a generic object. |
| | | 19 | | .NOTES |
| | | 20 | | This function is designed to work with PowerShell 7.0 and above. |
| | | 21 | | #> |
| | | 22 | | function Convert-HashtableToDictionary { |
| | | 23 | | param( |
| | | 24 | | [Parameter(Mandatory = $true)] |
| | | 25 | | [hashtable]$Data |
| | | 26 | | ) |
| | | 27 | | # Preserve original insertion order: PowerShell hashtable preserves insertion order internally |
| | 1 | 28 | | $ordered = [System.Collections.Specialized.OrderedDictionary]::new() |
| | 1 | 29 | | foreach ($k in $Data.Keys) { |
| | 2 | 30 | | $ordered.Add($k, (Convert-PSObjectToGenericObject $Data[$k])) |
| | | 31 | | } |
| | 1 | 32 | | return $ordered |
| | | 33 | | } |