如何使用powershell读取对象内部的值

问题描述 投票:0回答:1

我正在调用 REST API 来获取有关 azure 资源的一些指标数据。下面是命令,

$restcall = Invoke-RestMethod -Method GET -Uri $url -Headers $headers.这是其余调用的输出。

在上述命令之后,我尝试捕获数据内的值。下面是打印“Data”的命令。

$object = $restcall.value.timeseries.data

其余调用在循环中运行,并在数据部分提供不同的结果集。以下是每个循环的一些输出。时间戳是恒定的,但其他值在每次循环时都会发生变化。我如何用它们的值捕获第二个值?

azure powershell automation metrics
1个回答
0
投票

以下是每个循环的一些输出。时间戳是恒定的,但其他值在每次循环时都会发生变化。我如何用它们的值捕获第二个值?

我使用下面的脚本来获取第二个值:

$rithvalue=(Get-AzAccessToken -ResourceUrl "https://management.azure.com/").token          
$uri = "https://management.azure.com//subscriptions/b83c1ed/resourceGroups/AI/providers/Microsoft.CognitiveServices/accounts/aiud9/providers/Microsoft.Insights/metrics?api-version=2023-10-01"
$headers1 = @{
    "Authorization" = "Bearer $rithvalue"
    "Content-Type" = "application/json"
}  
$rithtest = Invoke-RestMethod -Method GET -Uri $uri -Headers $headers1 
$rithobj = $rithtest.value.timeseries.data 
foreach ($rithobj in $rithobj) {
    foreach ($key in $rithobj.PSObject.Properties) {
    if ($key.Name -ne "timestamp") {
            $keyName1 = $key.Name
            $value1 = $key.Value
            Write-Output "$keyName1 : $value1"
        }
}
}

Output:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.