Compare-Object - Separate side columns

后端 未结 1 1817
野的像风
野的像风 2021-01-26 08:27

Is it possible to display the results of a PowerShell Compare-Object in two columns showing the differences of reference vs difference objects?

For example

相关标签:
1条回答
  • 2021-01-26 09:17

    Possible? Sure. Feasible? Not so much. PowerShell wasn't really built for creating this kind of tabular output. What you can do is collect the differences in a hashtable as nested arrays by input file:

    $ht = @{}
    Compare-Object $Base $Test | ForEach-Object {
      $value = $_.InputObject
      switch ($_.SideIndicator) {
        '=>' { $ht['Test'] += @($value) }
        '<=' { $ht['Base'] += @($value) }
      }
    }
    

    then transpose the hashtable:

    $cnt  = $ht.Values |
            ForEach-Object { $_.Count } |
            Sort-Object |
            Select-Object -Last 1
    $keys = $ht.Keys | Sort-Object
    
    0..($cnt-1) | ForEach-Object {
      $props = [ordered]@{}
      foreach ($key in $keys) {
        $props[$key] = $ht[$key][$_]
      }
      New-Object -Type PSObject -Property $props
    } | Format-Table -AutoSize
    

    To include the item count in the header name change $props[$key] to $props["$key($($ht[$key].Count))"].

    0 讨论(0)
提交回复
热议问题