I am new to PowerShell, learning it on the fly coming from Python background.
I am pulling data from another tool where the data is retrieved through a REST call.
METERS variable is stored in this format in the source.
{
"500 HR": 500,
"1000 HR": 1000,
"2000 HR": 2000
}
PowerShell code
#REST call to source
$meter=@{}
Foreach ($item in $origresult.Items) {
$result = (Invoke-RestMethod -Uri $url -Headers $headers -Method GET -ContentType application/json -ErrorVariable RespErr)
$meter.Add($item.Name,$result.Value)
}
Write-Host ($meter | Out-String) -ForegroundColor Red
This is the Output
Name Value
---- -----
LastUploaded 2020-12-29T06:38:02
IsEnabled 1
METERS {...
ORGID WHS
How do I retrieve METERS and traverse through the dictionary? I tried this so far. Python spoiled me with its simple data structures, PowerShell is not so straightforward unless there is a simpler way to do this.
$mymeters = $meter.METERS | ConvertFrom-Json
Write-Host ($mymeters | Out-String) -ForegroundColor Yellow
Output
500 HR : 500
1000 HR : 1000
2000 HR : 2000
Here are somethings I have tried so far -
$mymeters = [ordered]@{}
" Here is the item $mymeters.Get_Item(500 HR)" #my silly attempt!
# Looping is a no go either - it says specialized ordered dictionary
foreach ($ind in $mymeters) {
" --> $ind"
}
Output
Here is the item System.Collections.Specialized.OrderedDictionary.Get_Item(500 HR)
--> System.Collections.Specialized.OrderedDictionary
I might be missing something real basic but I am unable to figure it out on my own! Any help is really appreciated. All I want is to traverse on the METERS hashtable/dictionary and call a function.