I query a REST API which returns a JSON with many subproperties that I need to extract using a calculated property hashtable with Select-Object
. The subproperty has always the same name (result
), and I want to use a filter or a function to avoid rewriting the hashtable all along.
Example:
some json | ConvertFrom-Json | Select-Object @{ Name="name"; Expression={ $_."name".result}}, @{ Name="year"; Expression={ $_."year".result}}, etc.
I thought it will be more convenient (and easy) to build a filter that would return the hashtable:
filter Get-Result {
param (
[string]$field
)
@{N=$field; E={ $_."$field".result}}
}
But $field is not replaced in Expression block. I don't understand why
> Get-Result -field "year"
Name Value
---- -----
N year
E $_."$field".result
How to make $_."$field".result
be $_."year".result
in Expression block?
Two options: closure or source code generation
1. Create a closure
You can call
GetNewClosure()
on the scriptblock passed to theExpression
entry - this will cause PowerShell's internal compiler to create a dynamic module that "remembers" the value bound to$fieldName
(or any other variable references to the scope whereGetNewClosure()
is called):2. Construct scriptblock from source code
The alternative is to construct the expression block by generating its source code and then passing that off to
[scriptblock]::Create()
: