DBremen
11/23/2015 - 9:39 AM

Proof of concept: Simplified syntax for calculated Properties with Select-Object

Proof of concept: Simplified syntax for calculated Properties with Select-Object

function mySelect {
    [CmdletBinding()]
    param(
    [Parameter(ValueFromPipeline=$true)]
    [psobject]$InputObject,
    [Parameter(Position=0)]
    $Property
    )
    begin{
        #only if the property array contains a hashtable property
        if ( ($Property | where { $_ -is [System.Collections.Hashtable] }) ) {
            $newProperty = @()
            foreach ($prop in $Property){
                if ($prop -is [System.Collections.Hashtable]){
                    foreach ($htEntry in $prop.GetEnumerator()){
                        $newProperty += @{n=$htEntry.Key;e=$htEntry.Value}
                    }
                }
                else{
                    $newProperty += $prop
                }
            }
            $PSBoundParameters.Property = $newProperty
        }
    }
    process{
        Select-Object @PSBoundParameters 
    }
}