How to extract used or free ips for all vnet with ip address space in powershell script

Saranya M 0 Reputation points
2023-03-27T11:19:14.5166667+00:00

Extract used or free ips for all vnet with subnet and also giving the ip address space for bith vnet by using powershell. and also get the result for extracting output in CSV file.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,044 questions
Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Entra | Microsoft Entra ID
{count} votes

2 answers

Sort by: Most helpful
  1. Sedat SALMAN 14,180 Reputation points MVP
    2023-03-27T12:27:00.9566667+00:00

    you can modify the following

    Connect-AzAccount
    
    # Get all VNets
    $vnets = Get-AzVirtualNetwork
    
    # Initialize an empty array for the results
    $results = @()
    
    
    foreach ($vnet in $vnets) {
    
        foreach ($subnet in $vnet.Subnets) {
    
            $usedIPs = (Get-AzNetworkInterface -VirtualNetwork $vnet).IpConfigurations | Where-Object { $_.Subnet.Id -eq $subnet.Id } | Select-Object -ExpandProperty PrivateIpAddress
    
    
            $ipAddressSpace = [System.Net.IPAddress]::Parse($subnet.AddressPrefix.Split("/")[0])
            $subnetMask = [Convert]::ToInt32($subnet.AddressPrefix.Split("/")[1])
            $ipRange = 1..([Math]::Pow(2, (32 - $subnetMask)) - 2)
    
    
            $freeIPs = $ipRange | ForEach-Object { $ipAddressSpace.Address += 1; $ipAddressSpace } | Where-Object { $_.ToString() -notin $usedIPs }
    
    
            $results += [PSCustomObject]@{
                "VNetName"        = $vnet.Name
                "VNetAddressSpace" = ($vnet.AddressSpace.AddressPrefixes -join ', ')
                "SubnetName"      = $subnet.Name
                "SubnetAddressSpace" = $subnet.AddressPrefix
                "UsedIPs"         = ($usedIPs -join ', ')
                "FreeIPs"         = ($freeIPs -join ', ')
            }
        }
    }
    
    # Export the results to a CSV file
    $results | Export-Csv -Path "VNetIPReport.csv" -NoTypeInformation
    
    

  2. KapilAnanth-MSFT 49,616 Reputation points Microsoft Employee Moderator
    2023-03-28T09:01:46.1266667+00:00

    @Anonymous

    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 know the number of Available IP Addresses and used IP Addresses in a subnet in VNet.

    # Get the VNet
    $resourceGroupName = "<RG Name>"
    $virtualNetworkName = "<VNET Name>"
    
    
    $virtualNetwork = Get-AzVirtualNetwork `
        -Name $virtualNetworkName `
        -ResourceGroupName $resourceGroupName
    
    #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"
    
    }
    
    

    Kindly let us know if this helps or you need further assistance on this issue.

    Thanks,

    Kapil


    Please don’t forget to close the thread by clicking "Accept the answer" wherever the information provided helps you, as this can be beneficial to other community members.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.