-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathConvertTo-OrderedDictionary.ps1
109 lines (97 loc) · 3.5 KB
/
ConvertTo-OrderedDictionary.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function ConvertTo-OrderedDictionary {
<#
.SYNOPSIS
Converts a HashTable, Array, or an OrderedDictionary to an OrderedDictionary.
.DESCRIPTION
ConvertTo-OrderedDictionary takes a HashTable, Array, or an OrderedDictionary
and returns an ordered dictionary.
If you enter a hash table, the keys in the hash table are ordered
alphanumerically in the dictionary. If you enter an array, the keys
are integers 0 - n.
.PARAMETER Hash
Specifies a hash table or an array. Enter the hash table or array,
or enter a variable that contains a hash table or array. If the input
is an OrderedDictionary the key order is the same in the copy.
.INPUTS
System.Collections.Hashtable
System.Array
System.Collections.Specialized.OrderedDictionary
.OUTPUTS
System.Collections.Specialized.OrderedDictionary
.NOTES
source: https://gallery.technet.microsoft.com/scriptcenter/ConvertTo-OrderedDictionary-cf2404ba
converted to function and added ability to copy OrderedDictionary
.EXAMPLE
$myHash = @{a=1; b=2; c=3}
ConvertTo-OrderedDictionary -Hash $myHash
Name Value
---- -----
a 1
b 2
c 3
.EXAMPLE
$myHash = @{a=1; b=2; c=3}
$myHash = .\ConvertTo-OrderedDictionary.ps1 -Hash $myHash
$myHash
Name Value
---- -----
a 1
b 2
c 3
$myHash | Get-Member
TypeName: System.Collections.Specialized.OrderedDictionary
. . .
.EXAMPLE
$colors = "red", "green", "blue"
$colors = .\ConvertTo-OrderedDictionary.ps1 -Hash $colors
$colors
Name Value
---- -----
0 red
1 green
2 blue
.LINK
about_hash_tables
#>
[CmdletBinding(ConfirmImpact = 'None')]
[OutputType('System.Collections.Specialized.OrderedDictionary')]
Param (
[parameter(Mandatory, ValueFromPipeline)]
[hashtable] $Hash
)
begin {
Write-Verbose -Message "Starting [$($MyInvocation.Mycommand)]"
}
process {
Write-Verbose -Message ($Hash.gettype())
if ($Hash -is [System.Collections.Hashtable]) {
Write-Verbose -Message '$Hash is a HashTable'
$dictionary = [ordered] @{}
$keys = $Hash.keys | Sort-Object
foreach ($key in $keys) {
$dictionary.add($key, $Hash[$key])
}
$dictionary
} elseif ($Hash -is [System.Array]) {
Write-Verbose -Message '$Hash is an Array'
$dictionary = [ordered] @{}
for ($i = 0; $i -lt $hash.count; $i++) {
$dictionary.add($i, $hash[$i])
}
$dictionary
} elseif ($Hash -is [System.Collections.Specialized.OrderedDictionary]) {
Write-Verbose -Message '$Hash is an OrderedDictionary'
$dictionary = [ordered] @{}
$keys = $Hash.keys
foreach ($key in $keys) {
$dictionary.add($key, $Hash[$key])
}
$dictionary
} else {
Write-Error -Message 'Enter a hash table, an array, or an ordered dictionary.'
}
}
end {
Write-Verbose -Message "Ending [$($MyInvocation.Mycommand)]"
}
}