I have a CSV file with two columns.
Sample Data:
Computername,Collection Name
MyServer01,NA - Data - SRV - Patching - Prod - Sat 18:00
MyServer02,NA - Data - SRV - Patching - Prod - Sat 22:00
MyServer03,NA - Data - SRV - Patching - Prod - Sat 02:00
MyServer04,NA - Data - SRV - Patching - Prod - Fri 18:00
MyServer05,NA - Data - SRV - Patching - Prod - Fri 02:00
MyServer06,NA - Data - SRV - Patching - Prod - Sun 18:00
I am looking to split the CSV into separate files along the collection. As the above example, Sat 18:00
So far I have the code:
$data=Import-Csv .\servers2.csv
$data | Where-Object {$_ -like '*fri*' -and $_.'Collection Name' -notmatch 'all.da.servers'} | Select ComputerName,'Collection Name'
The end goal is multiple text files based on the collection. So Servers-Fri-1800.txt and Fri-1800.txt. The server names will live in Servers*.txt and the Fri-1800.txt would contain the date in the format 12-11-15 18:00
I wanted to ask first because something tells me this method might be horribly inefficent. -Thanks!!
-Edit, the desired output. **Worth noting these text files will be read by another Powershell script, hence the reason for thinking a PSCustom object with the filtered properties would be ideal.
Servers-Sat-1800.txt contains MyServer01 and Sat-1800.txt contains 12-11-15 18:00
Servers-Sat-2200.txt contains MyServer02 and Sat-2200.txt contains 12-11-15 22:00
-edit 2, add code preview. Is there an easier way to accomplish this?
$data = import-csv .\file.csv
$data | Where-Object {$_ -like '*sat?18:00*' -and $_.'Collection Name' -notmatch 'ignore.string'} |
% {"{0}" -f $_.Computername} | out-file Servers-Sat-1800.txt
where-objectbut I'm unsure of how that would perform.