ConvertTo-Dictionary.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
<#
.SYNOPSIS
    Convert hashtable to generic dictionary.
.DESCRIPTION
 
.EXAMPLE
    PS C:\>ConvertTo-Dictionary @{ KeyName = 'StringValue' } -ValueType ([string])
    Convert hashtable to generic dictionary.
.INPUTS
    System.Hashtable
#>

function ConvertTo-Dictionary {
    [CmdletBinding()]
    [OutputType([System.Collections.Generic.Dictionary[object,object]])]
    param (
        # Value to convert
        [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
        [hashtable[]] $InputObjects,
        # Data Type of Key
        [Parameter(Mandatory=$false)]
        [type] $KeyType = [string],
        # Data Type of Value
        [Parameter(Mandatory=$false)]
        [type] $ValueType = [object]
    )

    process {
        foreach ($InputObject in $InputObjects) {
            $OutputObject = New-Object ('System.Collections.Generic.Dictionary[[{0}],[{1}]]' -f $KeyType.FullName,$ValueType.FullName)
            foreach ($KeyPair in $InputObject.GetEnumerator()) {
                $OutputObject.Add($KeyPair.Key, $KeyPair.Value)
            }

            Write-Output $OutputObject
        }
    }
}