Access row of array that has multiple values

Viewed 30

I have an array where the certificate columns has rows that contain multiple values. The ... indicates there are more than one value there.

For example, looking at the row for "mo.portalapp1.doris..." This is what I mean by multiple values being there.

enter image description here

enter image description here

I need to expand that row to see all four values as well as keep the expiration date which is obviously the same for all four. They are just essentially grouped together and need to un-group them almost. I tried using Select-Object -ExpandProperty Certificate and that does expand and show all the results, but then I lose the 'ExpirationDate' column. How can I accomplish this?

Thank you,

1 Answers

You can use Select-Object -ExpandProperty, but because the expanded value is a flat string (as opposed to a custom object with multiple properties), you need a bit more work to turn each value back into a Certificate property:

$array = $array |Select-Object * -ExcludeProperty Certificate -ExpandProperty Certificate |Select-Object @{Name='Certificate';Expression={"$_"}},* -ExcludeProperty Length

This might look a bit counter-intuitive, but the first Select-Object expands the string values from the Certificate property, and then attaches all remaining properties to the resulting values - we now have a list of strings with ETS properties attached to it.

The second Select-Object then creates a new object copying all the attached properties except for String.Length (ie. all the properties we copied from the original object), and re-attaches a Certificate property that contains the value of the string itself.

Let's test it on some dummy data:

$array = @(
  [pscustomobject]@{
    Certificate = @('cert1', 'cert2', 'cert3')
    ExpirationDate = '2022-01-05'
    Email = 'user@domain'
    Site = 'KY'
  }
  [pscustomobject]@{
    Certificate = @('cert4', 'cert5')
    ExpirationDate = '2022-01-10'
    Email = 'user@domain'
    Site = 'KY'
  }
)

$array |Format-Table

$array = $array |Select-Object * -ExcludeProperty Certificate -ExpandProperty Certificate |Select-Object @{Name='Certificate';Expression={"$_"}},* -ExcludeProperty Length

$array |Format-Table

Which shows the desired transformation:

Certificate           ExpirationDate Email       Site
-----------           -------------- -----       ----
{cert1, cert2, cert3} 2022-01-05     user@domain KY
{cert4, cert5}        2022-01-10     user@domain KY



Certificate ExpirationDate Email       Site
----------- -------------- -----       ----
cert1       2022-01-05     user@domain KY
cert2       2022-01-05     user@domain KY
cert3       2022-01-05     user@domain KY
cert4       2022-01-10     user@domain KY
cert5       2022-01-10     user@domain KY
Related