Home > OS >  find number of common servers count in multiple csv files powershell
find number of common servers count in multiple csv files powershell

Time:01-10

I have around 10-12 csv files in below format but number of records will be different

Start           Host    VMDiskLatency   Datastore    VM
1/7/2022 12:12  gen33   112              GEN_259b   
                                         GEN_319c   TN2AP
1/7/2022 12:12  gen34   294              GEN_259b
                                         GEN_319c   TN1AP
1/7/2022 12:12  gen36   170              GEN_319c   TN2WB

I have to find the common servers from all these csv files based on criteria

  1. A server appeared 5 or more time in 10 csv files

have to pick those records and put them in a separate csv file as result

e.g.

Start           Host    VMDiskLatency   Datastore    VM
1/7/2022 12:12  gen36   170              GEN_319c   TN2WB
1/7/2022 12:40  gen36   100              GEN_319c   TN2WB
1/7/2022 13:50  gen36   150              GEN_319c   TN2WB
1/7/2022 15:12  gen36   190              GEN_319c   TN2WB
1/7/2022 16:42  gen36   220              GEN_319c   TN2WB
1/7/2022 13:12  gen33   172              GEN_259b   
                                         GEN_319c   TN2AP
1/7/2022 14:12  gen33   212              GEN_259b   
                                         GEN_319c   TN2AP
1/7/2022 15:12  gen33   312              GEN_259b   
                                         GEN_319c   TN2AP
1/7/2022 16:12  gen33   412              GEN_259b   
                                         GEN_319c   TN2AP
1/7/2022 17:12  gen33   512              GEN_259b   
                                         GEN_319c   TN2AP
1/7/2022 17:52  gen33   232              GEN_259b   
                                         GEN_319c   TN2AP
1/7/2022 18:22  gen33   112              GEN_259b   
                                         GEN_319c   TN2AP

Need your help in getting some idea on how to do this. If any other information needed please do let me know.

CodePudding user response:

Something like this will work, but I haven't tested it. Import all csv files and group by name.

$allFiles = Import-Csv -Path  (Get-ChildItem -Path C:\CsvFiles\ -Filter '*.csv').FullName
$allFiles | Group-Object VM | Sort-Object Name | Select-Object Name, Count
  •  Tags:  
  • Related