Reputation: 91
I'm trying to find a way to list the amount of available IP addresses in an Azure vNet. I know this is available in the GUI, as can be seen below:
Question is, can we have the same information in the Powershell Az module?
Thanks!
Eitan
Upvotes: 2
Views: 4182
Reputation: 26315
From what I've seen, there is no direct way to fetch the number of available IP addresses from a subnet address range.
However, its easy enough to use the following formula:
2 ^ (32 - netmask length) - number of reserved Azure IPs - number of already occupied IPs
So if you had a subnet address range such as 10.1.0.0/24
, and one IP was already being occupied by another device, this would calculate to:
(2 ^ (32 - 24)) - 5 - 1 => (2 ^ 8) - 6 => 256 - 6 => 250
Furthermore, with PowerShell we could put these steps into code like this:
$resourceGroupName = "myResourceGroup"
$virtualNetworkName = "myResourceGroup-vnet"
$subnetName = "default"
$virtualNetwork = Get-AzVirtualNetwork `
-Name $virtualNetworkName `
-ResourceGroupName $resourceGroupName
$subnet = Get-AzVirtualNetworkSubnetConfig `
-VirtualNetwork $virtualNetwork `
-Name $subnetName
$subnetMask = $subnet.AddressPrefix.Split("/")[1]
$netmaskLength = [Math]::Pow(2, 32 - [int]$subnetMask)
$availableIpAddresses = $netmaskLength - 5 - $subnet.IpConfigurations.Count
$availableIpAddresses
Reference for the above Az module Cmdlets:
Additionally, have look at MSDN to see why Azure reserves 5 IP addresses specifically.
Upvotes: 2
Reputation: 222582
I came across this solution
,
$TableProperties = @(
@{Name="VNET" ; Expression={$VNET.Name}}
"Name"
"AddressPrefix"
@{Name="IPsConfigured" ; Expression={$SubnetConfigured.Count}}
@{Name="IPsLeft" ; Expression={$AvailableAddresses - $SubnetConfigured.Count}}
)
$Subnet |
Select-Object $TableProperties |
Format-Table -Autosize
Upvotes: 0