• 0
Cole McDonald

Rest API Raw Data Processing using Powershell

Question

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>

Share this post


Link to post
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.