Hello experts!
I have two csv files with multiple columns each (only two which are of importance) and have filtered and grouped the MID column (Machine-ID). Now each MID has a corresponding GB Storage Column whos values I want to add up for each group. I hope this was not too confusing. Here´s the example:
MID | GB Storage
A00001FE8 | 660
A00001FE8_SQL | 75
B0001458 | 1230
and so on… so I´ve grouped and filtered the MIDs already so A00001FE8 and A00001FE8_SQL are in one group. However now I also need to add up their Storage values. In this example 735 GB. How do I accomplish that?
My code so far:
#converting the .xls files to .csv for import
Function makeCSV($Excelfilename, $CSVfilename){
$xlCSV=6
$Excel = New-Object -comobject Excel.Application
$Excel.Visible = $False
$Excel.displayalerts=$False
$Workbook = $Excel.Workbooks.Open($ExcelFileName)
$Workbook.SaveAs($CSVfilename,$xlCSV)
$Excel.Quit()
If(ps excel){
kill -name excel
}
}
#call function to convert desired files to .csv
#param1 = filename and path to .xls file
#param2 = filename and path to .csv file that will be created
makeCSV “H:\Documents\Excel\cebrA-Cloud_Server_2.3_Skriptvorlage.xlsx” “H:\Documents\Excel\cebrA-Cloud_Server_2.3_Skriptvorlage.csv”
makeCSV “H:\Documents\Excel\2013_Q2_Kostenaufteilung_ISS-Server_skriptvorlage.xlsx” “H:\Documents\Excel\2013_Q2_Kostenaufteilung_ISS-Server_skriptvorlage.csv”
#import first csv file and multiply the GB TSM Storage values by 100
$csv = Import-Csv “H:\Documents\Excel\cebrA-Cloud_Server_2.3_Skriptvorlage.csv” -Delimiter “;”
$TSM = foreach($csvObj in $csv ){
$csvObj.'TSM-Storage'=100*($csvObj.'TSM-Storage').replace(',','.')
}
#import second csv file and divide the TSM Storage by 2 and by 1000
$csv2 = Import-Csv “H:\Documents\Excel\2013_Q2_Kostenaufteilung_ISS-Server_skriptvorlage.csv” -Delimiter “;”
$TSM2 = foreach($csvObj2 in $csv2 ){
$csvObj2.'TSM-Storage'=($csvObj2.'TSM-Storage').replace(',','.')/2/1000
}
$group = $csv | group {$.MID.substring(0,9)} #since these MIDs have the pattern A0000648_SQL and so on and I only need the first 8 characters for grouping
$group2= $csv2 | group{$.MID.substring(5,8)} #since these MIDs have the pattern M048_A48910EE_TDP and I only need the ID in between the “_”
$filePathes=“c:\test2.csv”,“c:\test.csv”
$summary=foreach ($filePath in $filePathes){
Import-Csv -Path $filePath | group {$.MID.substring(0,9)} | foreach {
$sum=($.Group.“GB TSM Storage” | Measure-Object -Sum).Sum
New-Object PSObject -Property @{“GB Sum”=$sum;MID=$_.Name}
}
}
the above code for the $summary was suggested by another forum member. However in my case (because the MID of one file needs to be trimmed at (0,9) and the other one at (5,8)) I get this output after some modifying (aside from some error messages):
$filePaths=“H:\Documents\Excel\2013_Q2_Kostenaufteilung_ISS-Server_skriptvorlage.csv”,“H:\Documents\Excel\cebrA-Cloud_Server_2.3_Skriptvorlage.csv”
$summary=foreach ($filePath in $filePaths){
Import-Csv -Path $filePath -Delimiter “;” | group {$group, $group2} | foreach {
$sum=($.Group.$TSM | Measure-Object -Sum).Sum
New-Object PSObject -Property @{“GB Sum”=$sum;MID=$.Name}
}
}
$summary | Sort-Object MID | ft -AutoSize
MID GB Sum
System.Object System.Object 0
System.Object System.Object 0