Private/ConvertTo-Long.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
function ConvertTo-Long {
    <#
    .SYNOPSIS
        "Melts" object(s) into an array of key-value pairs.
    .DESCRIPTION
        Converts object(s) wide objects into a long array object for better display.
    .PARAMETER obj
        The object(s) to melt.
    .PARAMETER KeyName
        The name of the resulting key column; defaults to "Name".
    .PARAMETER obj
        The name of the resulting value column; defaults to "Value".
    .INPUTS
        One or more objects.
    .OUTPUTS
        An array of objects with properties "$KeyName" and "$ValueName".
    #>

    param(
        [Parameter(ValueFromPipeline = $true)]
        [object[]]$obj,
        [ValidateNotNullOrEmpty()]
        [string]$KeyName = 'Name',
        [ValidateNotNullOrEmpty()]
        [string]$ValueName = 'Value'
    )
    
    process {
        foreach ($o in $obj) {
            $o.psobject.Properties | foreach { [pscustomobject]@{ $KeyName = $_.Name; $ValueName = $_.Value } }
        }
    }
}