1

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
4

1 に答える 1

1

csvにエクスポートする場合、考えられる解決策は、独自のPSObjectを作成し、そのプロパティを動的に設定することです。

このようなもの:

$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 ..

私はコードをテストしなかったので、いくつかの間違いがあるかもしれません。また、$tableの検証可能なタイプが正確に何であるかはわかりません。しかし、アイデアは明確でなければなりません。

于 2012-05-02T16:33:23.963 に答える