Forum Discussion

Cole_McDonald's avatar
Cole_McDonald
Icon for Professor rankProfessor
6 years ago

Rest API Raw Data Processing using Powershell

Here's a chunk of code for tokenizing the raw data returned from the REST API into something easier to use in powershell for processing / report generation.  $response is the return from the invoke-restmethod call:

<# Objectify Data Points #>
$dataCounters = @{}
$index        = 0
$dataPoints   = $response.data.dataPoints.Count

while ( $index -lt $response.data.time.count ) {
    $time             = $response.data.time[$index]
    $value            = $response.data.values[$index]
    $dataPointCounter = 0

    while ( $dataPointCounter -lt $dataPoints ) {
        
        $dataCounters[$response.data.dataPoints[$dataPointCounter]] += @(@{
            "time"  = $time
            "value" = $value[$dataPointCounter]
        })

        $dataPointCounter++
    }

    $index++
}

Now we can access an individual data point with $dataCounters.datasourcename... in my case, I was getting RAM info for a VM: $dataCounters.freePhsyicalMemory gets me an array of dictionaries that I can loop through:

foreach ( $counter in $dataCounters.freePhsyicalMemory ) {
	write-output "$($counter.time) - $($counter.value)"
}

Or filter with $dataCounters.freePhsyicalMemory | where time -gt 1553695107000

I can still access the full list of values or times via $dataCounters.freePhysicalMemory.Value or $dataCounters.freePhysicalMemory.Time

 

<Moderators: feel free to move this elsewhere.  I don't have access to the tips/tricks part of the board>