so I have this idea of a script which would combine multiple data exports from different sources when each of them contains only part of the information. The result should be longer CSV with first column values taken from list1 and second column would be values taken from the other CSVs. An example source CSVs:
list1.csv
server
hostname1
hostname2
hostname3
hostname4
hostname5
hostname6
hostname7
ADscan.csv
server;OS
hostname2;Microsoft Windows Server 2012 R2 Datacenter
hostname3;Microsoft Windows Server 2008 R2 Standard
export2.csv
server;OS
hostname1;w2k12
hostname2;w2k12
hostname3;w2k8
hostname4;w2k8
hostname5;w2k16
export3.csv
server;OS
hostname2.suffix;windows server 2012
hostname3.suffix;windows server 2008
hostname6.suffix;windows server 2008
The script should simply take each of the values from list1, look into other lists and assign .os property in case it is found. If not, look in the next list.
$list1=Import-Csv "list1.csv" -delimiter ";"
write-host "importing"($list1.count)"lines"
$list2=Import-Csv "ADscan.csv" -delimiter ";"
write-host "importing"($list2.count)"lines"
$list3=Import-Csv "export3.csv" -delimiter ";"
write-host "importing"($list3.count)"lines"
$list4=Import-Csv "export2.csv" -delimiter ";"
write-host "importing"($list4.count)"lines"
$list4.count
$result=@()
foreach($srv in $list1.server) {
$srv
$obj= [PSCustomObject]@{
server=$srv
OS=""
}
$obj.os=($list2|where {$_.server -eq $srv}|select os -first 1).os
if($obj.os -ne ""){$result+=$obj}
if(!$obj.os){ #this list contains .suffixes
$obj.os=($list3|where {$_.server -like $srv+".*"}|select os -first 1).os
if($obj.os){$result+=$obj}
}
if(!$obj.os){
$obj.os=($list4|where {$_.server -eq $srv}|select os -first 1).os
if($obj.os){$result+=$obj}
}
}
"RESULT"
$result|export-csv "result.csv" -NoTypeInformation -delimiter ";"
the resulting table should contain all values combined while line with hostname7 will have empty OS value as it's not in any list. It works, but is quite slow and not really usable when you have lists with thousands of lines. Any idea how to make this better and faster?