'Az Powershell function - how to get output in JSON format

I'm migrating / rewriting an azure cli script to Az Powershell. The current script includes logic like this:

#look up the storage account name for this resource group. 
$storageAccount = az storage account list -g $currentEnv.RESOURCEGROUP-o json | ConvertFrom-Json
Write-Output $storageAccount.name

Is there a way to do this using the Get-AzStorageAccount cmdlet? (version 4.1.1)

The cmdlet itself works ... I can use it to get the storage account info, but it comes back in table format. I would like JSON so I can ease the burden of parsing the results.



Solution 1:[1]

If you simply use the below command then you will get the storage account details in a table format which is the default format in Powershell as show in image:

Get-AzStorageAccount -ResourceGroupName <ResourceGroupName> 

enter image description here

But this doesn't mean that you can't use a single storage account name or print selected Paramters .

To print the Names you can use the below command :

Get-AzStorageAccount -ResourceGroupName <ResourceGroupName> | Select-Object StorageAccountName

enter image description here

And the same way you use Az CLI for using the names or a signle name from the list you can use the below commands:

$StorageAccount = Get-AzStorageAccount -ResourceGroupName <ResourceGroupName>
Write-Host $StorageAccount.StorageAccountName
Write-Host $StorageAccount.StorageAccountName[1]

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ansuman Bal