如果我的这两个文件包含以下数据集:
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)
为什么?如何获得所需的输出?不同的对象。
答案 0 :(得分:3)
Import-Csv
将返回PSCustomObject
数组,该数组具有名为CanonicalName
和LastSet
的属性。
当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 <=