How to list used IP address in azure

kedar giri 61 Reputation points
2024-04-06T01:55:53.8633333+00:00

I've been tasked with finding the total number of IPs used on a Vnet, but so far, I haven't found a solution. Has anyone encountered this requirement before or have experience dealing with it? Your insights would be greatly appreciated

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,158 questions
{count} votes

Accepted answer
  1. KapilAnanth-MSFT 35,251 Reputation points Microsoft Employee
    2024-04-12T04:08:29.06+00:00

    @kedar giri ,

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful