使用比较对象比较 Powershell 中的 csv 文件 [英] Comparing csv files in Powershell using Compare-Object

查看:63
本文介绍了使用比较对象比较 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

这篇关于使用比较对象比较 Powershell 中的 csv 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆