无法比较JSON(或自定义PS)对象

时间:2017-03-05 20:43:46

标签: powershell

我遇到对象比较问题。

我有一个应用程序可以吐出JSON数据。每次运行我的脚本时,我都会获取当前值并从上一次运行中导入存储的值(来自.json或.xml文件)。在脚本结束时,我将使用当前值覆盖存储的值。数据如下所示:

存储值:

id                  : 6549888
description         : Windows CPU via WMI
name                : Test
dataPoints          : {@{id=6314; dataSourceId=6549888; name=CPUBusyPercent; description=%
                  of Busy CPU; alertTransitionInterval=8; alertClearTransitionInterval=0; type=2; dataType=2;
                  maxDigits=4; postProcessorMethod=expression;
                  postProcessorParam=100-(PercentProcessorTime/100000); rawDataFieldName=; maxValue=; minValue=0;
                  userParam1=; userParam2=; userParam3=; alertForNoData=3; alertExpr=>= 50 60 70; alertSubject=CPU
                  alert on ##HOST##; alertBody=The host ##HOST## is in state ##LEVEL##. CPU is ##VALUE## percent
                  busy - it has been in this state since ##START##, or for ##DURATION##}}

当前值:

id                  : 6549888
description         : Windows CPU via WMI
name                : Test
dataPoints          : {@{id=6314; dataSourceId=6549888; name=CPUBusyPercent; description=%
                  of Busy CPU; alertTransitionInterval=8; alertClearTransitionInterval=0; type=2; dataType=2;
                  maxDigits=4; postProcessorMethod=expression;
                  postProcessorParam=100-(PercentProcessorTime/100000); rawDataFieldName=; maxValue=; minValue=0;
                  userParam1=; userParam2=; userParam3=; alertForNoData=3; alertExpr=>= 90 95 98; alertSubject=CPU
                  alert on ##HOST##; alertBody=The host ##HOST## is in state ##LEVEL##. CPU is ##VALUE## percent
                  busy - it has been in this state since ##START##, or for ##DURATION##}}

在此示例中,有人将alertExpr从“50 60 70”更改为“90 95 98”,我需要捕获它(以及对其余属性所做的更改)。

当我运行foreach循环时,我没有回到任何差异,但是当我查看单个属性时,Powershell会报告差异。所以以下什么都不返回:

foreach ($dataSource in $currentDataSources) {
    foreach ($recordedDatasource in $previousDataSources) {
        If ($dataSource.id -eq $recordedDatasource.id) {
            Compare-Object $dataSource $recordedDatasource
        }
    }
}

但是看看索引1034(这是我的测试项目)确实显示出不同之处:

Compare-Object $currentDataSources[1034].datapoints.alertexpr $previousDatasources[1034].datapoints.alertexpr

检查我的数据集之间的每个属性的最佳方法是什么?

感谢。

0 个答案:

没有答案