Welcome to the Microsoft Q&A Platform. Thank you for reaching out & I hope you are doing well.
I understand that you would like to find the total number of IPs used on a Vnet.
The below Powershell script should help you :
# Get the VNet
$resourceGroupName = "<RG Name>"
$virtualNetworkName = "<VNET Name>"
$virtualNetwork = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName
#Overall VNET values
$TotalUsedIPAddresses=0
$TotalAvailableIPAddresses=0
#Loop through the VNet for individual Subnets
foreach ($subnet in $virtualNetwork.Subnets)
{
$subnetMask = $subnet.AddressPrefix.Split("/")[1]
$netmaskLength = [Math]::Pow(2, 32 - [int]$subnetMask)
$availableIpAddresses = $netmaskLength - 5 - $subnet.IpConfigurations.Count
Write-Host "Subnet Name: " $subnet.Name
Write-Host "Address Range: " $subnet.AddressPrefix
Write-Host "Total usable IP Addresses in this Subnet: " ($netmaskLength - 5)
Write-Host "Used IP Addresses: " $subnet.IpConfigurations.Count
Write-Host "Available IP Addresses: " $availableIpAddresses
Write-Host "`n"
#Sum Overall VNET values
$TotalUsedIPAddresses = $TotalUsedIPAddresses + $subnet.IpConfigurations.Count
$TotalAvailableIPAddresses = $TotalAvailableIPAddresses + $availableIpAddresses
}
#Display Overall VNET values
Write-Host "TotalUsedIPAddresses: " $TotalUsedIPAddresses
Write-Host "TotalAvailableIPAddresses: " $TotalAvailableIPAddresses
- This lists the "Used IP Addresses" and "Available IP Addresses" grouped by subnet.
- Then adds the individual subnet's values throughout the loop to arrive at the overall VNET values.
Please let us know if we can be of any further assistance here.
Thanks,
Kapil
Please Accept an answer if correct.
Original posters help the community find answers faster by identifying the correct answer.