I’m using az vm list-skus -l australiasoutheast | convertfrom-json to populate a variable (lets call it $skus) so I can use it in a PS script. When $skus is populated, it’s contents look like:
apiVersions :
capabilities : {@{name=MaxResourceVolumeMB; value=256000}, @{name=OSVhdSizeMB; value=1047552}, @{name=vCPUs; value=8}, @{name=MemoryPreservingMaintenanceSupported; value=False}…}
capacity :
costs :
family : standardMSFamily
kind :
locationInfo : {@{location=australiasoutheast; zoneDetails=System.Object[]; zones=System.Object[]}}
locations : {australiasoutheast}
name : Standard_M8-4ms
resourceType : virtualMachines
restrictions : {}
size : M8-4ms
tier : Standard
The capabilities and locatioinfo objects in the variable look like hashtables.
I can do $skus | select name, capabilities which outputs:
name capabilities
---- ------------
Aligned {@{name=MaximumPlatformFaultDomainCount; value=2}}
Classic {@{name=MaximumPlatformFaultDomainCount; value=3}}
Premium_LRS {@{name=MaxSizeGiB; value=4}, @{name=MinSizeGiB; value=0}, @{name=MaxIOps; value=120}, @{name=MinIOps; value=120}…}
Premium_LRS {@{name=MaxSizeGiB; value=128}, @{name=MinSizeGiB; value=64}, @{name=MaxIOps; value=500}, @{name=MinIOps; value=500}…}
Premium_LRS {@{name=MaxSizeGiB; value=256}, @{name=MinSizeGiB; value=128}, @{name=MaxIOps; value=1100}, @{name=MinIOps; value=1100}…}
How can I get to and extract the data I need from those hashtables?
I tried the usual select-object to try and get to it, but I can only get as far as the capabilities object, but I cannot reference anything inside that.
2
Answers
To retrieve all capabilities from a hash table, use
Select-Object
to print the specific value from the capabilities parameter.Here is the PowerShell script to reference the value from capabilities parameter.
Output:
To display all values within capabilities, you can utilize the following cmdlet to print only name and values.
Output:
Without knowing the exact structure and data of the object-graph, it is difficult to give you a specific answer (see also how to ask and providing a mvce) but based on this example (note that
Value
is actually the top node) and this Object-Graph tools set, I can give you a general view and answer:What is the path (reference) to each (leaf) node in this graph-object?
Each path that is listed above might be used the get to the specific node in the object-graph, e.g.:
$Skus.
value[0].capabilities[0].name
returns:
As you probably want to get programmatically (dynamically) to this property you might use the
Invoke-Expression
command to get to the value of this property:But as mentioned in this helpful related answer from mklement0:
So, you might also consider to use the object-graph tools to safely get to the specific node:
This will give you a node (
PSNode
class) in return:Or using a more advanced Extended Dot-Notation (Xdn) path:
To get to the
Value
contained by the node:The node value refers to the same value as you original object-graph. Meaning, you might simply change the value like this:
To confirm this, check your original
$Skus
object: