如何获得两个两个元素数据集之间的不同对象?

时间:2019-12-07 21:28:28

标签: powershell active-directory command-behaviors

如果我的这两个文件包含以下数据集:

OldFile:

"CanonicalName","LastSet"
"CONTOSO.com/NA/US/OU1/Users/Active/Test User","2019-07-02 14:14:44"
"CONTOSO.com/NA/US/OU1/Users/Active/User One","2019-07-02 14:14:44"
"CONTOSO.com/NA/US/OU2/Users/Active/User Two","2018-09-02 05:53:35"
"CONTOSO.com/OC/AU/OU3/Users/User Three","2017-06-23 14:20:07"

TempFile:

"CanonicalName","LastSet"
"CONTOSO.com/NA/US/OU1/Users/Active/User One","2019-07-02 14:14:44"
"CONTOSO.com/NA/US/OU2/Users/Active/User Two","2018-09-02 05:53:35"
"CONTOSO.com/OC/AU/OU3/Users/User Three","2017-06-23 14:20:07"

返回:

InputObject                                                                                  SideIndicator
-----------                                                                                  -------------
@{CanonicalName=CONTOSO.com/OC/AU/OU3/Users/User Three; LastSet=2017-06-23 14:20:07} <=

在执行以下代码时,我希望它返回“ <=”的TEST用户:

Compare-Object -ReferenceObject (import-csv $oldfile) -DifferenceObject (import-csv $tempfile) 

为什么?如何获得所需的输出?不同的对象。

1 个答案:

答案 0 :(得分:3)

Import-Csv将返回PSCustomObject数组,该数组具有名为CanonicalNameLastSet的属性。

Compare-Object必须将复杂对象与属性进行比较时,您需要使用-Property参数来告诉它在进行相等性测试时要比较哪些属性:

Compare-Object -ReferenceObject (Import-Csv $oldfile) -DifferenceObject (Import-Csv $tempfile) -Property CanonicalName,LastSet

结果如下:

CanonicalName                                LastSet             SideIndicator
-------------                                -------             -------------
CONTOSO.com/NA/US/OU1/Users/Active/Test User 2019-07-02 14:14:44 <=