Vnet Subnet

Handian Sudianto 4,116 Reputation points
2023-03-24T08:40:14.1+00:00

Hello,

Can we know and export if possible list of resources (VM, APP service etc.) for specific subnet?

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,140 questions
{count} votes

Accepted answer
  1. KapilAnanth-MSFT 35,001 Reputation points Microsoft Employee
    2023-03-27T09:01:32.4233333+00:00

    @Handian Sudianto

    You can use the below script to get the list of connected NICs, Private EndPoints and VNet Integrated resources for all the subnets in the VNet.

    $Vnet = Get-AzVirtualNetwork -Name <VNet Name> -ResourceGroup <RG Name>
    
    foreach ($subnet in $Vnet.Subnets){
    
    Write-Host "Name: " $subnet.Name
    Write-Host "Address Range: " $subnet.AddressPrefix
    Write-Host "`n"
    
    Write-Host "List of Private EndPoints:"
    foreach ($pe in $subnet.PrivateEndPoints){
    $pe.Id
    }
    Write-Host "`n"
    
    Write-Host "List of Connected NICs:"
    foreach ($nic in $subnet.IpConfigurations){
    $nic.Id
    }
    Write-Host "`n"
    
    Write-Host "VNet Integration"
    foreach ($sal in $subnet.ServiceAssociationLinks ){
    $sal.Link
    }
    Write-Host "`n"
    
    Write-Host "End of Subnet" 
    Write-Host "`n"
    
    }
    

    Kindly let us know if this helps or you need further assistance on this issue.

    Thanks,

    Kapil


    Please don’t forget to close the thread by clicking "Accept the answer" wherever the information provided helps you, as this can be beneficial to other community members.


1 additional answer

Sort by: Most helpful
  1. Sedat SALMAN 13,160 Reputation points
    2023-03-24T10:07:47.0066667+00:00

    yes you can do by using the following azure cli code block

    -- note install azure cli before running code block

    # Log in to your Azure account
    az login
    # Set your subscription if you have multiple subscriptions
    az account set --subscription "<YourSubscriptionID>"
    # Retrieve the specific subnet's ID
    subnet_id=$(az network vnet subnet show --name "<SubnetName>" --vnet-name "<VnetName>" --resource-group "<ResourceGroupName>" --query "id" -o tsv)
    # List resources associated with the specific subnet using the subnet ID
    resources=$(az resource list --query "[?contains(properties.subnet.id, '${subnet_id}')]" -o json)
    # Export the list of resources to a JSON file
    echo $resources > resources_in_subnet.json
    # Optional: Export the list of resources to a CSV file
    az resource list --query "[?contains(properties.subnet.id, '${subnet_id}')].{Name:name, Type:type, Location:location}" -o table > resources_in_subnet.csv
    
    

    also with the following powershell code block

    # Log in to your Azure account
    Connect-AzAccount
    # Set your subscription if you have multiple subscriptions
    Set-AzContext -SubscriptionId "<YourSubscriptionID>"
    # Retrieve the specific subnet's ID
    $subnet = Get-AzVirtualNetworkSubnetConfig -Name "<SubnetName>" -VirtualNetwork (Get-AzVirtualNetwork -ResourceGroupName "<ResourceGroupName>" -Name "<VnetName>")
    $subnetId = $subnet.Id
    # List resources associated with the specific subnet using the subnet ID
    $resources = Get-AzResource | Where-Object { $_.Properties.Subnet -and $_.Properties.Subnet.Id -eq $subnetId }
    # Export the list of resources to a JSON file
    $resources | ConvertTo-Json -Depth 4 | Set-Content -Path "resources_in_subnet.json"
    # Optional: Export the list of resources to a CSV file
    $resources | Select-Object -Property Name, ResourceType, Location | Export-Csv -Path "resources_in_subnet.csv" -NoTypeInformation