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