perniciouscaffeine
perniciouscaffeine

Reputation: 121

Can azure CLI get a list of all resources running in a tenant?

Is there a single command way to get all the resources in a tenant dumped out in JSON format, either in az cli or powershell?

Upvotes: 5

Views: 11687

Answers (2)

Scott McNeany
Scott McNeany

Reputation: 572

The code below will loop through all subscriptions and output all resources to a CSV file in the current directory.

$accountList = az account list
$accountObjectList = $accountList | ConvertFrom-Json
$accountObjectList | ForEach-Object -Process {
               $subscriptionId = $_.id
               $subscriptionName = $_.name
               az account set --subscription $_.id
               $resourceList = az resource list --output json --query "[].{name:name, resourceGroup:resourceGroup, type:type, sku:sku.name, kind:kind, location:location }"
               $resourceObjectList = $resourceList | ConvertFrom-Json
               $resourceObjectList | ForEach-Object -Process {
                         $_ | Add-Member -NotePropertyName SubscriptionId -NotePropertyValue $subscriptionId
                         $_ | Add-Member -NotePropertyName SubscriptionName -NotePropertyValue $subscriptionName

               }
               $resourceObjectList | Export-Csv -Path .\azureResourceList.csv -Append
}

Upvotes: 4

James Wood
James Wood

Reputation: 17562

Azure CLI

az resource list

Powershell

Get-AzureRmResource

Upvotes: 5

Related Questions