【发布时间】:2019-10-21 14:57:44
【问题描述】:
有 2 个 CSV 文件,其中包含 4 列。显示名称、WindowsEmailAddress、部门、职务。我希望脚本告诉我任何差异,包括新用户是否出现在一张纸上或被删除。有一些成功告诉我一些改变,但不能让它输出改变的整行。因此,一个用户的部门发生了变化,我希望它输出显示名称、电子邮件地址,这样我就知道它是为谁更改的。
使用下面的这个脚本取得了最大的成功。但输出不正确。有什么想法吗?
$newemp = Import-Csv -Path "C:\temp\departmentlist.csv" -Header department |
Select-Object "department"
$ps = Import-Csv -Path "C:\temp\departmentlist2.csv" -Header department |
Select-Object "department"
#get list of (imported) CSV properties
$props1 = $newemp | gm -MemberType NoteProperty | select -Expand Name | sort
$props2 = $ps | gm -MemberType NoteProperty | select -Expand Name | sort
#first check that properties match
#omit this step if you know for sure they will be
if (Compare-Object $props1 $props2) {
throw "Properties are not the same! [$props1] [$props2]"
} else {
#pass properties list to Compare-Object
Compare-Object $newemp $ps -Property $props1
}
【问题讨论】:
-
将
-PassThru添加到Compare-Object调用,将输出通过管道传输到Where-Object {$_.SideIndicator -eq '=>'}。 otta 会为您提供第二个集合中与第一个集合不匹配的对象。
标签: powershell compare