转换PSObject列

时间:2017-02-22 17:59:21

标签: powershell

我正在尝试从我正在创建的数据集中“折叠”行。 “站点”列中的值是我想要的列。

以下是现在表格的示例以及我希望它的外观:

$RowA1 = @{ObjectPath="Setting1";CtrlValue="ABC";TestValue="DEF";Site="clientA"}
$RowA2 = @{ObjectPath="Setting2";CtrlValue="123";TestValue="456";Site="clientA"}
$RowA3 = @{ObjectPath="Setting1";CtrlValue="ABC";TestValue="GHI";Site="clientB"}
$RowA4 = @{ObjectPath="Setting2";CtrlValue="123";TestValue="789";Site="clientB"}
$Source = $RowA1,$RowA2,$RowA3,$RowA4
$Source | %({[PSCustomObject]$_}) | select ObjectPath,CtrlValue,TestValue,Site | Format-Table -AutoSize

Write-Host "--------------------------------------------------------"

$RowB1 = @{ObjectPath="Setting1";CtrlValue="ABC";clientA="DEF";clientB="GHI"}
$RowB2 = @{ObjectPath="Setting2";CtrlValue="123";clientA="456";clientB="789"}
$Dest = $RowB1,$RowB2
$Dest | %({[PSCustomObject]$_}) | select ObjectPath,CtrlValue,clientA,clientB | Format-Table -AutoSize



ObjectPath CtrlValue TestValue Site   
---------- --------- --------- ----   
Setting1   ABC       DEF       clientA
Setting2   123       456       clientA
Setting1   ABC       GHI       clientB
Setting2   123       789       clientB


--------------------------------------------------------

ObjectPath CtrlValue clientA clientB
---------- --------- ------- -------
Setting1   ABC       DEF     GHI    
Setting2   123       456     789  
  1. 如何动态执行此类转换(第一个数据集中可能还有clientC)?
  2. 这种转换的名称是什么(可能是分区)?
  3. 我可以通过编程方式创建所需的结果集,但我想在使用数据集时在Powershell中使用更多查询样式的指令。

1 个答案:

答案 0 :(得分:1)

按标识该组的属性进行分组,然后通过循环遍历组成员来添加列。例如:

$RowA1 = @{ObjectPath="Setting1";CtrlValue="ABC";TestValue="DEF";Site="clientA"}
$RowA2 = @{ObjectPath="Setting2";CtrlValue="123";TestValue="456";Site="clientA"}
$RowA3 = @{ObjectPath="Setting1";CtrlValue="ABC";TestValue="GHI";Site="clientB"}
$RowA4 = @{ObjectPath="Setting2";CtrlValue="123";TestValue="789";Site="clientB"}
$RowA5 = @{ObjectPath="Setting1";CtrlValue="ABC";TestValue="JKL";Site="clientC"}
$Source = $RowA1,$RowA2,$RowA3,$RowA4,$RowA5 | % ({[PSCustomObject]$_})

#Identifier-propteries
$idprop = "ObjectPath", "CtrlValue"

$Dest = $Source |
#Group objects that belong together using identifier-properties
Group-Object -Property $idprop | ForEach-Object {
    #Create new object with common properties
    $obj = $_.Group[0] | Select-Object $idprop

    #Add client-properties
    $_.Group | ForEach-Object {
        Add-Member -InputObject $obj -MemberType NoteProperty -Name $_.Site -Value $_.TestValue
    }

    #Output new "consolidated" object
    $obj

}

#PowerShell only displays the columns in the first object by default. Workaround: Specify all columns we want
#Find all propertynames in collection
$cols = $Dest | % { $_.psobject.properties } | Select-Object -ExpandProperty Name -Unique

#Output using Select-Object
$Dest | Select-Object -Property $cols

输出:

ObjectPath : Setting1
CtrlValue  : ABC
clientA    : DEF
clientB    : GHI
clientC    : JKL

ObjectPath : Setting2
CtrlValue  : 123
clientA    : 456
clientB    : 789
clientC    : 


$Dest | Format-Table -Property $cols -AutoSize

ObjectPath CtrlValue clientA clientB clientC
---------- --------- ------- ------- -------
Setting1   ABC       DEF     GHI     JKL    
Setting2   123       456     789