Use variable for property name in calculated property
i writing code read xml file , dynamically generate report field names exported csv. i've got working, except can't life of me figure out how use variable name reference property in expression select-object
i'm trying like:
foreach ($entry in $xml') { $currentfieldset = @{ name = $entry.objectname; expression = { $_.$($entry.$reportname) } } } $reportfieldmappings += $currentfieldset
later on i'd use $reportfieldmappings following:
$objects | select-object $reportfieldmappings | export-csv "c:\path\to\some\file.csv"
it has me absolutely stumped. no matter in script block:
expression = { $_.$($entry.$reportname) }
i can't ever reference correct property name report. i hope explanation makes sense. i can provide more detail if necessary. any appreciated!!
you try subexpression.
$_.$($reportname)
i've never attempted this, don't know if work or not.
edit: seems work:
$propvar = 'lastwritetime' get-item .\1.txt | select name,@{n='test';e={$_.$($propvar)}} name test ---- ---- 1.txt 10/30/2015 8:48:08 am
Windows Server > Windows PowerShell
Comments
Post a Comment