使用比较对象比较 Powershell 中的 csv 文件
我是 powershell 的新手,我在创建用于比较两个 csv 文件的脚本时遇到了一些困境.第一个 csv 只有一个名为数据库名称"的列.第二个 csv 有很多列,我只关心其中两个数据库名称"和主机名".现在脚本只比较数据库名称"列,效果很好!!并导出到 Differences.csv.但是,我还想查看差异文件中每个数据库名称"对应的主机名"列.
I am new to powershell and I having a little dilemma with a script I created to compare two csv files. The first csv has only one column called "Database Name" in it. The secound csv has a many columns and I only care about two of them "Database Name" and "Host Name". Right now the script compares only the "Database Name" column which works great!! and exports to Differences.csv. However, I would also like to see the corresponding "Hostname" column for each "Database Name" in the difference file.
$northdb = Import-Csv -Path ".\northdb.csv" -Header "Database Name" | Sort-object Property "Database Name" -Unique
$sdb = Import-Csv ".\Current\SQLDatabaseInventory.csv" -Header "hostname",h2,h3,h4,h5,"Database Name" |Sort-Object -Property "Database Name" -Unique
Compare-Object $northdb $sdb -Property "Database Name" | Where-Object{$_.SideIndicator -eq '=>'} |
Select-Object "Database Name" | export-csv .\Difference.csv -NoTypeInfo
请帮忙
如果您将 -PassThru
添加到您的比较对象,您将收到所有对象属性
If you add the -PassThru
to your compare object you'll receive all the object properties
Compare-Object $northdb $sdb -Property "Database Name" -PassThru
然后你也可以导出主机名:
Then you can also export hostname :
Compare-Object $northdb $sdb -Property "Database Name" -PassThru | Where-Object{$_.SideIndicator -eq '=>'} | Select-Object "Database Name", "hostname" | export-csv .\Difference.csv -NoTypeInfo