Script to derive all the subnets in Azure.

Om Bhosle 60 Reputation points
2024-12-18T07:32:12.6866667+00:00

$vnetList = Get-AzVirtualNetwork

foreach ($vnet in $vnetList) {

Write-Host "Virtual Network: $($vnet.Name), Resource Group: $($vnet.ResourceGroupName)"

# Get all subnets in the virtual network

$subnetList = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet

foreach ($subnet in $subnetList) {

    Write-Host "  Subnet Name: $($subnet.Name), Address Prefix: $($subnet.AddressPrefix)"

}

Write-Host ""

}

Hello all,
I found this script online on microsoft for deriving all the subnets in the Azure Vnets. This helps in deriving the subnet if the data is huge. I needed only one help, if any could add a line item to represent the data in tabular form please help. Also if we could the data of which services are deployed in the subnets, that would help too.

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,574 questions
0 comments No comments
{count} votes

Accepted answer
  1. Deepanshu katara 12,475 Reputation points
    2024-12-18T08:37:10.3933333+00:00

    Hello Om , Welcome to MS Q&A

    To derive all subnets in Azure Virtual Networks (VNets) and represent the data in a tabular form, you can use Azure Resource Graph queries. The following query retrieves the names of the VNets, subnet names, address prefixes, and other relevant information:

    Resources
    | where type == 'microsoft.network/virtualnetworks'
    | extend subnets = properties.subnets
    | mv-expand subnets
    | project name, subnets.name, subnets.properties.addressPrefix, location, resourceGroup, subscriptionId
    
    
    
    
    
    
    
    

    You can run this query in Azure Resource Graph Explorer to get the desired information. However, the query does not directly include services deployed in the subnets. To gather information about services, you may need to perform additional queries or use Azure's management tools to correlate resources with their respective subnets.

    Also another thing could be using the addition in script

    Please find updated script

    $vnetList = Get-AzVirtualNetwork
    
    # Create a list to store the data for tabular output
    $data = @()
    
    foreach ($vnet in $vnetList) {
        Write-Host "Virtual Network: $($vnet.Name), Resource Group: $($vnet.ResourceGroupName)"
        
        # Get all subnets in the virtual network
        $subnetList = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $vnet
    
        foreach ($subnet in $subnetList) {
            Write-Host "  Subnet Name: $($subnet.Name), Address Prefix: $($subnet.AddressPrefix)"
            
            # Get Network Interfaces (NICs) in the subnet
            $nics = Get-AzNetworkInterface | Where-Object { $_.IpConfigurations.Subnet.Id -eq $subnet.Id }
            
            $services = @()
            foreach ($nic in $nics) {
                # Get the associated service name (e.g., VM name)
                $vmName = ($nic.VirtualMachine.Id -split '/')[-1]
                if ($vmName) {
                    $services += $vmName
                } else {
                    $services += "Other Service (e.g., App Gateway)"
                }
            }
            
            # Add the data to the array
            $data += [pscustomobject]@{
                VirtualNetwork = $vnet.Name
                ResourceGroup  = $vnet.ResourceGroupName
                SubnetName     = $subnet.Name
                AddressPrefix  = $subnet.AddressPrefix
                Services       = $services -join ", "
            }
        }
    }
    
    # Output the data as a table
    $data | Format-Table -AutoSize
    
    # Optionally export the data to a CSV file
    $data | Export-Csv -Path "VNet_Subnet_Services.csv" -NoTypeInformation -Encoding UTF8
    
    

    References:

    Please let us know if any questions

    Kindly accept answer if it helps

    Thanks

    Deepanshu


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.