| | | 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 an OrderedHashtable to an OrderedDictionary, converting any nested PSObjects to generic objects. |
| | | 10 | | .DESCRIPTION |
| | | 11 | | This function takes an OrderedHashtable as input and converts it to an OrderedDictionary. It ensures that any nested |
| | | 12 | | The order of keys in the OrderedHashtable is preserved in the resulting OrderedDictionary. |
| | | 13 | | .PARAMETER Data |
| | | 14 | | The OrderedHashtable to convert. |
| | | 15 | | .EXAMPLE |
| | | 16 | | $oht = [ordered]@{ "Key1" = "Value1"; "Key2" = [PSCustomObject]@{ Prop1 = "Val1"; Prop2 = "Val2" } } |
| | | 17 | | $od = Convert-OrderedHashtableToDictionary $oht |
| | | 18 | | $od 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-OrderedHashtableToDictionary { |
| | | 23 | | param( |
| | | 24 | | [Parameter(Mandatory = $true)] |
| | | 25 | | [System.Collections.Specialized.OrderedDictionary] $Data |
| | | 26 | | ) |
| | 2 | 27 | | foreach ($i in $($data.PSBase.Keys)) { |
| | 1 | 28 | | $Data[$i] = Convert-PSObjectToGenericObject $Data[$i] |
| | | 29 | | } |
| | 1 | 30 | | return $Data |
| | | 31 | | } |