Join-Object two different csv files using PowerShell

℡╲_俬逩灬. 提交于 2019-12-25 19:07:34

问题


The first .csv file is an monthly backup size in KB on based client name. The second .csv file is an next monthly backup size in KB on based client name.

It lists all the Client Name in column A. Column B has the corresponding policy name of client and last column backup size in KB (i.e. - 487402463).

If the difference between client size (1638838488 - 1238838488 = 0.37 in TB ) is greater than 0.10 TB , the results will be spit out in TB size to a csv file like below.

Also , a client may be related multiple policy name.

My question is : I want to add something too.

  • Backup size may decrease in the next month such as hostname15,Company_Policy_11.
  • Also , hostname55,Company_Policy_XXX may have different policy name.
  • hostnameXX,Company_Policy_XXX,0 and hostnameXX,Company_Policy_XXX,41806794 it may be duplicate client and policy name. if this does not exist in CSV2 then I want to display as negative (-0.14) like below. Or may be exist in CSV2 hostnameZZ,Company_Policy_XXX as well.
  • Lastly just it may be in CSV2 such as hostnameSS,Company_Policy_XXX. I used the Join-Object module. https://github.com/ili101/Join-Object

Example CSVFile1.csv

Client Name,Policy Name,KB Size
hostname1,Company_Policy,487402463
hostname2,Company_Policy,227850336
hostname3,Company_Policy_11,8360960
hostname4,Company_Policy_11,1238838488
hostname15,Company_Policy_11,3238838488
hostname1,Company_Policy_55,521423110
hostname10,Company_Policy,28508975
hostname3,Company_Policy_66,295925
hostname5,Company_Policy_22,82001824
hostname2,Company_Policy_33,26176885
hostnameXX,Company_Policy_XXX,0
hostnameXX,Company_Policy_XXX,141806794
hostnameYY,Company_Policy_XXX,121806794
hostname55,Company_Policy_XXX,41806794
hostnameZZ,Company_Policy_XXX,0
hostnameZZ,Company_Policy_XXX,141806794

Example CSVFile2.csv

Client Name,Policy Name,KB Size
hostname1,Company_Policy,487402555
hostname2,Company_Policy,227850666
hostname3,Company_Policy_11,8361200
hostname4,Company_Policy_11,1638838488
hostname1,Company_Policy_55,621423110
hostname15,Company_Policy_11,1238838488
hostname10,Company_Policy,28908975
hostname3,Company_Policy_66,295928
hostname5,Company_Policy_22,92001824
hostname2,Company_Policy_33,36176885
hostname22,Company_Policy,291768854
hostname23,Company_Policy,291768854
hostname55,Company_Policy_BBB,191806794
hostnameZZ,Company_Policy_XXX,0
hostnameZZ,Company_Policy_XXX,291806794
hostnameSS,Company_Policy_XXX,0
hostnameSS,Company_Policy_XXX,291806794

Desired Output :

Client Name,Policy Name,TB Size
hostname4,Company_Policy_11,0.37
hostname22,Company_Policy,0.27
hostname23,Company_Policy,0.27
hostnameYY,Company_Policy_XXX,-0.12
hostnameXX,Company_Policy_XXX,-0.14
hostname15,Company_Policy_11,-2
hostname55,Company_Policy_BBB,0.15
hostnameZZ,Company_Policy_XXX,0.15
hostnameSS,Company_Policy_XXX,0.29

Here is my script so far :

$CSV2 | FullJoin $CSV1 `
    -On 'Client Name','Policy Name' `
    -Property 'Client Name',
              'Policy Name', 
              @{'TB Size' = {[math]::Round(($Left.'KB Size' - $Right.'KB Size') * 1KB / 1TB, 2)}} | 
    Where-Object  {[math]::Abs($_.'TB Size') -gt 0.10} | Export-Csv C:\Toolbox\DataReport.csv -NoTypeInformation

回答1:


You could so something similar to the following. This assumes you want to subtract CSV1 values from CSV2 values.

# Read CSV files and make CSV1 sizes negative. Makes summing totals simpler.
$1 = Import-Csv CSVFile1.csv | Foreach-Object { $_.'KB Size' = -$_.'KB Size'; $_ }
$2 = Import-Csv CSVFile2.csv

# Calculated Properties to be used with Select-Object
$CalculatedProperties = @{n='Client Name';e={$_.Group.'Client Name' | Get-Unique}},
                        @{n='Policy Name';e={$_.Group.'Policy Name' | Get-Unique}},
                        @{n='TB Size';e={[math]::Round(($_.Group.'KB Size' | Measure -Sum).Sum*1KB/1TB,2)}}

# Grouping objects based on unique client and policy name combinations
$1 + $2 | Group-Object 'Client Name','Policy Name' |
    Select-object $CalculatedProperties |
        Where {[math]::Abs($_.'TB Size') -gt 0.10}


来源:https://stackoverflow.com/questions/58864302/join-object-two-different-csv-files-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!