根据 PowerShell 中 Invoke-RestMethod 的结果计算值

Calculate values from results of Invoke-RestMethod in PowerShell

我可以通过 Invoke-RestMethod 从提供以下格式的 API 取回数据:

    1612142668000000000 : @{peak_performance=29.18; current_utilization=19.15}
    1612146268000000000 : @{peak_performance=29.05; current_utilization=20.03}
    1612149868000000000 : @{peak_performance=29.07; current_utilization=18.86}

如果有帮助,上面的JSON格式是:

{
  "1612142668000000000": {
    "peak_performance": 29.18,
    "current_utilization": 19.15
  },
  "1612146268000000000": {
    "peak_performance": 29.05,
    "current_utilization": 20.03
  },
  "1612149868000000000": {
    "peak_performance": 29.07,
    "current_utilization": 18.86
  }
}

我希望能够计算并显示所有可用的 peak-performancecurrent_utilization 值的平均值,但我不知道该怎么做。有什么想法吗?

有很多解决方案:

$json = @"
{
  "1612142668000000000": {
    "peak_performance": 29.18,
    "current_utilization": 19.15
  },
  "1612146268000000000": {
    "peak_performance": 29.05,
    "current_utilization": 20.03
  },
  "1612149868000000000": {
    "peak_performance": 29.07,
    "current_utilization": 18.86
  }
}
"@

$jout = $json | ConvertFrom-Json
$result = $jout.PSObject.Properties.Value | Measure-Object -Property current_utilization,peak_performance -Average

foreach($v in $result){
    Write-Host $v.Property   " = "  $v.Average
}

结果:

current_utilization  =  19.3466666666667
peak_performance  =  29.1