User
User

Reputation: 59

Get the Services and it names List By Location in Azure Subscription

I'm able to get all the available locations in my current subscription using REST API now iam trying to get the services and it names in specified location including Resources Groups, Vnets, Vm's etc using python sdk's or powershell. Is there any possible way to achieve the same Assist me to resolve or the Documents. Thanks in Advance! [![enter image description here][1]][1] list all resources available in the azure subscription based on the location here location should be param.


from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient

def list_resources_by_location(subscription_id, location):
    # Authenticate with Azure using DefaultAzureCredential
    credential = DefaultAzureCredential()

    # Initialize the ResourceManagementClient
    resource_client = ResourceManagementClient(credential, subscription_id)

    # Get resources by location
    resources = resource_client.resources.list_by_location(location)

    # Print resource details
    print(f"List of resources in location '{location}':")
    for resource in resources:
        print(f"ID: {resource.id}")
        print(f"Name: {resource.name}")
        print(f"Type: {resource.type}")
        

subscription_id = "subscription_id"

# Specify the location you want to list resources from
location = "westus2"


list_resources_by_location(subscription_id, location)

Below example in EastUS i have 50 Vnets created need that list for other services aswell [1]: https://i.sstatic.net/ZCB8R.png

Upvotes: 0

Views: 113

Answers (1)

wenbo
wenbo

Reputation: 1461

A simple way to achieve this is using Resource Graph shared query using Azure PowerShell (link)

  • step 1: install Az.ResourceGraph moudle
  • step 2: write a query
  • step 3: search the query

Here is a sample code to query all vnet in eastus. no need to loop all resources. just like a simple query in sql table.

query.ps1

#install if firstly use.
#Install-Module -Name Az.ResourceGraph

# Store the query in a variable
$query = @"
resources
| project name, type, location, resourceGroup
| where type == 'microsoft.network/virtualnetworks' and location == 'eastus'
| order by name asc
"@

# Run Azure Resource Graph query with `order by` first, then with `limit`
Search-AzGraph -Query $query

Result:

enter image description here

what you should do:

Modify the query to meet your requirements, such as location == 'westus' ,etc

Upvotes: 0

Related Questions