H20 – hashtable from scriptblock to powershell Objects
April 21st, 2011 by KarlHere is another function in my endless Quest at producing Helper functions that simplify doing complicated stuff in PowerShell and that keeps it in the pipeline.
function h20([scriptblock]$sb, [switch]$dontkeepreference = $false)
{
begin {}
process{ if ($sb -ne $null)
{
$local:obj = $_;
$local:ht = &$sb;
if ($local:ht -is [hashtable])
{
if (!$dontkeepreference) { $local:ht.".." = $local:obj; }
New-Object PSObject -Property $local:ht
}
}
if ($local:ht -is [object[]])
{
$local:ht | where { $_ -is [hashtable]} | % {
if (!$dontkeepreference) {$_.".." = $local:obj}
New-Object PSObject -Property $_ }
}
}
end{}
}
Now this function processes the scriptblock for each item coming through the pipeline giving you the opportunity to create one or more Hashtables, and it will take whatever Hashtables you create and create nice PSCustomObjects and pop them out the other side of the pipeline.
Here are some examples
1..10 | h20 { @{karl = $_;dude = $_+1} }
modify the incoming data with an expression
gps | h20 { @{name = $_.processname; mem = $_.workingset / 1MB} }
easily flatten data with subexpressions
get-command -verb get | h20 { @{ name = $_.name; type = $_.commandtype; synopsis = ($_ | get-help).synopsis ; } }
don’t lose the incoming pipeline data when you don’t explicitly include it in the hashtable. Its automatically stored in the property “..”
below, while the expression just includes string properties from the get-help object, the actual commandinfo object is stored in “..”
$a = get-command -verb ("get","set") | h20 { $_ | get-help | % { @{name = $_.name ; synopsis = $_.synopsis} } }
a[0].".."
Posted in Powershell | No Comments »
