Partager via


Listing all subnets in an Azure Virtual Network

Listing all Subnets in an Azure Virtual Network

 

So you want to list ALL the subnets within an Azure Virtual Network and you try to look for an available PowerShell command (get-azuresubnet only works at a VM level) and you realize there isn’t any. Fret not, there is a simple way to get this done.

 

The Virtual Network configuration file is your friend.

 

$VNetConfig = Get-AzureVNetConfig

[xml]$VNetConfigXML = $VNetConfig.XMLConfiguration

 

$subnets = $VNetConfigXML.DocumentElement.VirtualNetworkConfiguration.VirtualNetworkSites.VirtualNetworkSite.Subnets

 

Your $subnets parameter now has all the Subnet information, you can filter it by an IF statement if you just need the subnets from one of the Virtual Network Sites.

If you then run a loop, you will be able to fetch all the subnet names and the IP address prefix within an Azure VNet

 

foreach ($subnet in $subnets)

 

{

 

$subnet.Subnet

 

}

 

Where would this be helpful? When you want to set up an NSG rule or forced tunneling for all the subnets, this comes in handy. I will do a separate post on NSGs and forced tunneling later on to explain in detail

 

Happy PowerShelling

Anand Kumar R

Comments

  • Anonymous
    December 12, 2017
    Can you tell me how we will get the list of subnets without using powershell?
    • Anonymous
      December 18, 2017
      you could also get it through Virtual Network configuration file or from the portal - if thats what you mean ?