Home > Software engineering >  Az query to list all subscriptions, resource groups and resources in it
Az query to list all subscriptions, resource groups and resources in it

Time:02-07

I am trying to iterate over all azure subscriptions, resource groups and resources

CodePudding user response:

You can use the below PowerShell script to pull the list of all subscriptions & their resource groups & resources in it.

Connect-AzAccount
$subs=Get-AzSubscription
foreach($sub in $subs){

  Set-AzContext -Subscription $sub.id -Force
  $resourcegroups= Get-AzResourceGroup
  Write-Host "Currently connected  to subscription"$sub.id -ForegroundColor red -BackgroundColor white

  foreach($rg in $resourcegroups){

    Write-Host "Current resourcegroup"$rg.ResourceGroupName -ForegroundColor Yellow -BackgroundColor Black
    Write-Host "Here are list of resources present in this resource group:" -ForegroundColor Cyan 
    $resource=Get-AzResource -ResourceGroupName $rg.ResourceGroupName
    Write-Output $resource.ResourceName
    

  }
}

Here is the sample Output for reference:

enter image description here

CodePudding user response:

An easier way of doing it would be to use the enter image description here

  •  Tags:  
  • Related