Scott Lezberg Scott Lezberg - 1 year ago 100
Git Question

Powershell - Getting multiple values from a nested object

I am sure this is a very basic PowerShell question but I have a JSON file that I am loading into a PowerShell custom object with

and the following

$JsonContent.value | Select-Object -Property Name, @{Name = 'commit'; Expression = { $ }}

I am trying to get another column in the nested commit object and the following is NOT working so am hoping someone can help me figure out how to get a second value from the nested commit object (I have tried a few variations but not working)

JsonContent.value | Select-Object -Property Name, @{Name = 'commit'; Expression = { $}, {$_.commit.commitId}}



Answer Source

The part after Expression = ... is a script block containing code so if you want to return an array of values there for being the value for the property commit then just modify

Expression = { $}, {$_.commit.commitId}  


Expression = { $, $_.commit.commitId}

so that you use the commma operator to create and return your 2-element array

Probably you rather want to make some nice string representing the commit e.g.

Expression = { "{0} - {1}" -f $, $_.commit.commitId }

In case you want the commitId in a completely different column just add another property like so

... -Property Name, @{Name = 'commit'; Expression = { $}}, @{Name = 'commitId'; Expression = {$_.commit.commitId}} 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download