Home > Mobile >  Using Powershell Dictionary
Using Powershell Dictionary

Time:01-27

I struggle with very basic powershell scripting.

I have an object "ids" (I cant tell exactly what type it is) that translates to json like this

[{"id": "3ccbfe7a-e381-ed11-81ad-000d3aba6139","RowKey": "204640","PartitionKey": "ppm"},
 {"id": "7339255d-e381-ed11-81ad-000d3aba6139","RowKey": "205269","PartitionKey": "ppm"}]

I simply want to get the "ids" by "rowkey". e.g.

    $ids["204640"] shall resolve to "3ccbfe7a-e381-ed11-81ad-000d3aba6139".

It is obvious that this should be very easy - but I fail ... My understanding is, that it is a list or array of objects with named properties. I assume some simple casting magic is necessary...

(my debugging skills are very limited because I am a Powershell newbie and I do the scripting in the azure portal, trying to write an Azure Function using a table binding. actually "ids" comes from that binding.)

CodePudding user response:

It is unclear if your Json string has already been converted into objects or not, for that you can use ConvertFrom-Json. Then you can use Group-Object -AsHashtable grouping on RowKey property:

$string = '
[
  {"id": "3ccbfe7a-e381-ed11-81ad-000d3aba6139","RowKey": "204640","PartitionKey": "ppm"},
  {"id": "7339255d-e381-ed11-81ad-000d3aba6139","RowKey": "205269","PartitionKey": "ppm"}
]
'

$hash = (ConvertFrom-Json $string) | Group-Object RowKey -AsHashTable
$hash['205269'].id # => 7339255d-e381-ed11-81ad-000d3aba6139
  • Related