Pregunta

is there a way how to expand / flatten row in datatable? I have data in variable $table which has 2 columns. In first columns there is stored [string] value, while in second column there is stored [string[]] array value, which contains at least 2 values. Here comes example of one such row:

DistinguishedName                       GroupNames
-----------------                       ----------
Applications/FarmName/Notepad           {Domain\Group1, Domain\Group2}

I'd like to have it flattened and exported to e.g. CSV format, while first column keeps "key value" DistinguishedName and all other columns are filled with particular value expanded from GroupNames column. Example of desired output follows:

Applications/FarmName/Notepad; Domain\Group1; Domain\Group2
¿Fue útil?

Solución

If your intention is exporting to csv, possible solution might be creating own PSObject and dynamicaly populating it's properties.

Something like this:

$expandedObjects = @()
$table | % {
    #assuming $_ reffers to actual row

    $obj = new-object PSObject
    $obj | Add-Member -MemberType NoteProperty -Name "DistinguishedName" -Value $_.DistinguishedName

    $i = 0 # for dynamic property naming
    $_.GroupNames | % {
        # assuming $_ reffers to actual GroupName value
        $obj | Add-Member -MemberType NoteProperty -Name $("GroupName{0}" -f $i++) -Value $_
    }
    $expandedObjects += $obj
}
$expandedObjects | export-csv ..

I didn't test the code, so there might be some mistakes. Also I don't know what type exactly your $table veriable is. But the idea should be clear.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top