Private/ConvertFrom-KeyValuePair.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
function ConvertFrom-KeyValuePair {
    <#
.SYNOPSIS
Takes object containing key/value pair
Returns Property=Value object

.DESCRIPTION
Converts:
Key Value
--- -----
PropertyName False

Into:
UnlockAccounts : False


.PARAMETER KeyValue
The object(s) containing the key / value pair data

.EXAMPLE
$object | ConvertFrom-KeyValuePair

Returns Key=Value object members for each matching entry in $object

#>

    [CmdletBinding()]
    param (
        [parameter(
            Mandatory = $true,
            ValueFromPipeline = $true
        )]
        [ValidateNotNullOrEmpty()]
        [object[]]$KeyValue
    )

    begin {

        $OutputObject = [PSCustomObject]@{ }

    }

    process {

        If ($null -ne $KeyValue) {

            $KeyValue | ForEach-Object {

                If ($null -ne $PSItem.key) {

                    $OutputObject | Add-Member -Name $($PSItem.key) -Value $($PSItem.value) -MemberType NoteProperty

                }
            }
        }
    }

    end {
        $OutputObject
    }

}