Easily create multiple subnets in an Azure Virtual Network

I recently needed to create a whole set of subnets in a large number of virtual networks of various sizes. I thought some variables would be a great way to quickly create the set of subnets in each virtual network which were each /20 networks in a shared class B IP which enabled 16 virtual networks per Class B IP space. The goal was to show that each subnet didn’t need to be a full class C (/24) in instead we could use smaller subnets based on the number of hosts actually required. I’ve included the comments which explains the subnets created and the number of hosts supported in each.

#10.x.y.0/26            Gateway subnet     reserved for gateway, no hosts
#10.x.y.64/26           Subnet1        59 usable
#10.x.y.128/26          Subnet2        59 usable
#10.x.y.192/26          Subnet3        59 usable
#10.x.y+1.0/25          Subnet4        123 usable
#10.x.y+1.128/25        Subnet5        123 usable
#10.x.y+2.0/24          Subnet6          251 usable
#10.x.y+3.0/27          Subnet7        27 usable
#10.x.y+3.32/27         Subnet8        27 usable
#10.x.y+3.64/27         Subnet9        27 usable
#10.x.y+3.96/27         Subnet10       27 usable
#10.x.y+3.128/26        Subnet11       59 usable
#10.x.y+3.192/26        Subnet12       59 usable
#As can be seen subnets can be a variety of sizes based on the number of IPs required in each. It is recommended to use the
#smallest subnet possible to optimize IP address space
#3rd octet increase by 16 for each virtual network

$BaseCIDR = "" #This would change for each network
$IPSplit = $BaseCIDR.Split(".")
$First2Octet = "$($IPSplit[0]).$($IPSplit[1])"
$3rdOctet = [int]$IPSplit[2]

#Set the NSG, VNet and resource group names as variables $NSGName, $VNetName and $VNetRG

$NSG = Get-AzureRmNetworkSecurityGroup -Name $NSGName -ResourceGroupName $VNetRG
$VNet = Get-AzureRmVirtualNetwork -Name $VNetName -ResourceGroupName $VNetRG

$GWSubnet ="$First2Octet.$3rdOctet.0/26"
Write-Output "Creating GW Subnet: $GWSubnet"

$subnets = "$First2Octet.$3rdOctet.64/26","$First2Octet.$3rdOctet.128/26","$First2Octet.$3rdOctet.192/26","$First2Octet.$($3rdOctet+1).0/25",

foreach($subnet in $subnets)
    Write-Output "Creating subnet $subnet"
    Add-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $VNet -Name "Subnet$subnetno" `
        -AddressPrefix $subnet -NetworkSecurityGroup $NSG

Set-AzureRmVirtualNetwork -VirtualNetwork $VNet

Write-Output "A /24 gives 251 usable, a /25 gives 123 usable, /26 gives 59 usable and a /27 gives 27 usable"
Write-Output "These subnets are created as defaults and to give examples. You can create additional subnets up to $First2Octet.$($3rdOctet+15).x"



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: