Search code examples
azuresubscription

To list Azure resource for all subscriptions in an azure account


I need to list the Azure SQL servers in an Azure account for all subscriptions. I need to list them for all subscriptions in one call or is there any other way to achieve this like using script for the Azure account other than API calls? Please help. It would be really helpful if the script is also provided.

I know this is possible, using Azure rest API, but we could list it only for one subscription at a time. But I am expecting to have the response for all subscriptions at the same time.


Solution

  • Thanks @Peter Bons for the comment.

    Yes, you can check the results in Resource Graph Explorer.

    If you want to go with CloudShell, check the below Bash script to get the list of SQL resources.

    Open the Azure Cloud Shell, enter image description here

    • You need to have read permissions on other subscriptions to list the resources.

    • In Bash, run the below command to get the list of SQL query

     az sql server list
    
    • First get all the subscriptions into a variable
    sub_ids=$(az account list --all --query '[].id' -o tsv)
    
    • Loop the Subscription ID to get the list of SQL Servers.
    sub_ids=$(az account list --all --query '[].id' -o tsv)
    for sub_id in $sub_ids; 
    do
    echo "List of SQL Servers for Subscription ID: $sub_id"     
      
    az sql server list --subscription $sub_id --query '[].{SQLServer: name, ResourceGroup: resourceGroup, Location: location}' --output table
        
    done
    

    Output: enter image description here

    As I don't have authorization on other subscription, Iam getting the below error.

    enter image description here

    Make sure you have required permissions to read other subscriptions.