Home > Enterprise >  How to list down azure resources inside a particular subscription through powershell?
How to list down azure resources inside a particular subscription through powershell?

Time:09-27

How to list down all the azure resources viz App services , App service plans , apim , service bus ,cosmos through powershell based on the subscription

The agenda here is to fetch these resources by passing the subscription as a parameter and getting all the respective resources inside that particular subscription.

I know how to fetch the resources using the resource group name as the parameter ,for eg

az appservice plan list [--resource-group]   

But here I do not want to fetch based on the resource group , I need to pass the subscription so that all the resources inside that subscription get listed

CodePudding user response:

list down azure resources inside a particular subscription through powershell

In PowerShell using PowerShell Commands:

Firstly, you need to set azure subscription using below PowerShell command:

xxx = Can be subcription name or id

Set-AzContext -Subscription "xxx"

Output:

enter image description here

Then use below PowerShell command to get all resources in the current Subdcription:

Get-AzResource | ft

enter image description here

Output:

enter image description here

ft means format table

CodePudding user response:

Using the cli

az account set -s production
$Resources = az resource list | ConvertFrom-Json

$FilterType = @('Microsoft.Web/sites', 'Microsoft.ServiceBus/namespaces', 'Microsoft.DocumentDb/databaseAccounts')
$Resources | Where Type -in $FilterType

Using the Az module

Select-AzSubscription -Subscription 'Production'
$Resources = Get-azresource

$FilterType = @('Microsoft.Web/sites', 'Microsoft.ServiceBus/namespaces', 'Microsoft.DocumentDb/databaseAccounts')
$Resources | Where ResourceType -in $FilterType

AS for viewing just the types so you can make your selection, you can look in the portal URL of the resource or get all types present in your subscription using

# Query all resource first...

# Az cli
$Resources | Select -ExpandProperty Type -Unique | Sort-Object

# Az module
$Resources | Select -ExpandProperty ResourceType -Unique | Sort-Object
  • Related