Last active
August 16, 2022 00:47
-
-
Save DBremen/d33f3851cc54e2d5cfbf to your computer and use it in GitHub Desktop.
Proof of concept: Simplified syntax for calculated Properties with Select-Object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment