2

I'm trying to list all my Azure VMs with their sizes using a powershell command. Problem is that the HardwareProfile property returns a JSON object, that I would like to parse and use only the vmSize property value of that object.

So I'm running this command:

Get-AzureRmVM

Which gives me this:

ResourceGroupName        : TESTRG
...
Name                     : ubuntu-server
...
HardwareProfile          : {
                             "vmSize": "Standard_DS2"
                           }
...

NOTE the JSON in the HarwareProfile value.

What I want to do is:

Get-AzureRmVM | Select ResourceGroupName, Name, HardwareProfileText `
              | Out-Gridview -PassThru

Which works - only, I would like to get rid of the JSON notation in the HardwareProfileText. Using Format-Table looks like so:

ResourceGroupName Name          HardwareProfileText
----------------- ----          -------------------
TESTRG            ubuntu-server {...

So the question is: how can I get only the value of vmSize in this table ? Can I sneak ConvertFrom-Json in somewhere?

0

2 Answers 2

5

Can't you use the select-expression directly and convert the json-string into an object? So you can use it later in your pipeline.

Something like :

select @{Name="VMSize";Expression={($_|ConvertFrom-Json).vmSize}};

Given your json as a text in a file (for a simple-test):

(Get-Content -raw C:\tmp\test.json)|select @{Name="VMSize";Expression={($_|ConvertFrom-Json).vmSize}};

This will give you a property with only the VmSize. You can combine the expression select with normal properties as well, or multiple expressions and then continue to pass it down the pipeline if you want to filter on additional criteria.

Sign up to request clarification or add additional context in comments.

Comments

0

I don't know the get-azureRmVm function but it works just fine with the property InstanceSize instead of HardwareProfileText.

Import-Module 'Azure'
Get-AzureVM | Select ResourceGroupName, Name, InstanceSize `
          | Out-Gridview -PassThru

Result

1 Comment

All the Rm functions are from the 'new' ResourceManager model functions - I'll check whether the interface is different from the 'classic' / ASM model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.