Skip to content

Instantly share code, notes, and snippets.

@DBremen
Last active August 16, 2022 00:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DBremen/d33f3851cc54e2d5cfbf to your computer and use it in GitHub Desktop.
Save DBremen/d33f3851cc54e2d5cfbf to your computer and use it in GitHub Desktop.
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment