I am calling REST API to get some metric data on azure resource. Below is the command,
$restcall = Invoke-RestMethod -Method GET -Uri $url -Headers $headers. Here is the output of the rest call.
After the above command I am trying to capture the values inside data. Below is the command to print "Data".
$object = $restcall.value.timeseries.data
The rest call runs on loop and provide different set of result in the data section. Here are few output of each loop. The timestamp is constant but the other value gets changed on every loop. How can I capture the 2nd value with their value?
Here is the complete code which I am using:
Select-AzSubscription -SubscriptionId $subId
#variable:
$metricname = "GeneratedTokens", "TokenTransaction","ProcessedPromptTokens", "Latency"
$aggtype = "Total", "Average", "Maximum"
$timespan = "2023-12-25T11:05:00.000Z/2024-02-14T11:05:00.000Z"
#Get OpenAI accounts
$res = Get-AzCognitiveServicesAccount
$openaiAcc = $res | Where-Object {$_.Endpoint -match 'openai.azure.com/'}
#generate bearer token:
$accesstoken = (Get-AzAccessToken).Token
$headers = @{
'Authorization' = "Bearer $accesstoken"
}
foreach($openai in $openaiAcc){
$openaiId = $openai.id
$subscriptionId = $openaiId.split('/')[2]
$RGName = $openai.ResourceGroupName
foreach($metric in $metricname){
Write-Host "the metric name is $metric"
foreach($agg in $aggtype){
$url = "https://management.azure.com/$openaiId/providers/Microsoft.Insights/metrics?timespan=$timespan&metricnames=$metric&aggregation=$agg&api-version=2023-10-01&interval=FULL"
$restcall = Invoke-RestMethod -Method GET -Uri $url -Headers $headers
$object = $restcall.value.timeseries.data
}
}
}
Here are few output of each loop. The timestamp is constant but the other value gets changed on every loop. How can I capture the 2nd value with their value?
I have got value with timeStamp and total:
I have used below script to get second value:
$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: