Resource Graph Query To Get All Resources Within Azure V-Net

Anonymous
2023-06-16T09:28:34.6866667+00:00

Hi,
I hope this message finds you well. I'm currently facing an issue regarding the retrieval of all resources within a specific Virtual Network (VNet) in Azure. I have attempted to use Azure Resource Graph queries to accomplish this task but haven't been successful so far.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,061 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sourabh Gupta 795 Reputation points Microsoft Vendor
    2024-05-26T10:12:56.56+00:00

    Hi

    Thanks for reaching out.

    Could you please try the following query.

    Resources | where type =~ 'Microsoft.Network/networkInterfaces' | extend vnetId = tostring(properties.ipConfigurations[0].properties.subnet.id) | where vnetId has 'YOUR_VNET_NAME' | project id, name, type, location, resourceGroup
    

    and pass this query as body of the post request at following API end point

    POST https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2021-03-01
    

    Below is the sample code in python

    
    import requests 
    import json 
    
    query = { "query":
    "Resources
     | where type =~ 'Microsoft.Network/networkInterfaces' | extend vnetId = tostring(properties.ipConfigurations[0].properties.subnet.id) | where vnetId has 'YOUR_VNET_NAME' | project id, name, type, location, resourceGroup"
     } 
    
     url = 'https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2021-03-01'
     headers = { 'Authorization':f'Bearer {access_token}',
                    'Content-Type':'application/json'}
    
    response = requests.post(url, headers=headers, data=json.dumps(query))
    if
     response.status_code == 200:
     resources = response.json() print("Resources
     in the VNet:")
    for
     resource in
     resources['data']:
    print(resource)
    else:
    print(f"Error:{response.status_code}")
    print(response.json())
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments