![](/img/trans.png)
[英]Extracting data from api using powershell tool 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-performance
和current_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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.