Combine two tables that are similar with two columns different using powershell

前端 未结 1 642
走了就别回头了
走了就别回头了 2021-01-23 11:38

I need to join two tables that I have created in Powershell. The problem is that out of the 5 columns they share, two columns are different in each table. How can I join these

1条回答
  •  粉色の甜心
    2021-01-23 12:10

    Ok, assuming you don't have linked entries and all the records are separate you could do this:

    $T2Keys = $Results2|gm|?{$_.MemberType -match "Property"}|Select -ExpandProperty Name
    $T1Keys = $Results|gm|?{$_.MemberType -match "Property"}|Select -ExpandProperty Name
    $KeysToAdd = $T2Keys|?{$T1Keys -notcontains $_}
    $Results3 = @()
    $Results3 += $Results
    $KeysToAdd|%{$Results3|Add-Member $_ ""}
    $Results3+=$Results2
    

    That queries the properties of the first table to a variable. It does the same to the second table. It finds the properties that are in the second table that aren't in the first one and saves them to a variable. Then it makes an empty array, adds the first table to it, adds the missing fields, then adds the second table to it.

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