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 255.255.255.192 reserved for gateway, no hosts #10.x.y.64/26 Subnet1 255.255.255.192 59 usable #10.x.y.128/26 Subnet2 255.255.255.192 59 usable #10.x.y.192/26 Subnet3 255.255.255.192 59 usable #10.x.y+1.0/25 Subnet4 255.255.255.128 123 usable #10.x.y+1.128/25 Subnet5 255.255.255.128 123 usable #10.x.y+2.0/24 Subnet6 255.255.255.0 251 usable #10.x.y+3.0/27 Subnet7 255.255.255.224 27 usable #10.x.y+3.32/27 Subnet8 255.255.255.224 27 usable #10.x.y+3.64/27 Subnet9 255.255.255.224 27 usable #10.x.y+3.96/27 Subnet10 255.255.255.224 27 usable #10.x.y+3.128/26 Subnet11 255.255.255.192 59 usable #10.x.y+3.192/26 Subnet12 255.255.255.192 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 = "10.240.32.0/20" #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", "$First2Octet.$($3rdOctet+1).128/25","$First2Octet.$($3rdOctet+2).0/24","$First2Octet.$($3rdOctet+3).0/27","$First2Octet.$($3rdOctet+3).32/27", "$First2Octet.$($3rdOctet+3).64/27","$First2Octet.$($3rdOctet+3).96/27","$First2Octet.$($3rdOctet+3).128/26","$First2Octet.$($3rdOctet+3).192/26" $subnetno=1 foreach($subnet in $subnets) { Write-Output "Creating subnet $subnet" Add-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $VNet -Name "Subnet$subnetno" ` -AddressPrefix $subnet -NetworkSecurityGroup $NSG $subnetno++ } 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"