bashazureazure-devopsazure-cliazure-diagnostics

Azure CLI / bash: is there a way to list all diagnostic settings of a resource-group / subscription


I'm looking for a way to list all "Diagnostic Settings"-Objects of a resource-group or subscription with the azure cli / bash. I've read the azure cli documentation which can only list diagnostic settings by resource-type.

I thought before I'll give up I'll ask the whole swarm power of stackoverflow. Maybe some guy had the same probleme and wrote a script which iterates to all resources of a subscription and list the "Diagnostic Settings"-Resources


Solution

  • You could always get the list of resource ids and iterate:

    # list of resource ids for the subscription
    resourceIds=$(az resource list --query "[].id" --output tsv)
    
    # or filter on a specific resource group
    resourceGroup="<resource group name>"
    resourceIds=$(az resource list --query "[].id" --resource-group $resourceGroup --output tsv)
    
    # iterates and print diag settings
    for id in $resourceIds
    do
      az monitor diagnostic-settings list --resource $id
    done
    

    equivalent powerhsell script:

    # list of resource ids for the subscription
    $resourceIds = az resource list --query "[].id" | ConvertFrom-Json
    
    # or filter on a specific resource group
    $resourceGroup="<resource group name>"
    $resourceIds = az resource list --query "[].id" --resource-group $resourceGroup | ConvertFrom-Json
    
    # iterates and print diag settings
    foreach($id in $resourceIds){
      az monitor diagnostic-settings list --resource $id
    }