PowerShell Azure – Get First Available IP From IP Range

| |

If you build virtual machines in Azure you may find this script useful.

Currently it’s not the fastest it could likely be if you have a large subnet but it works for my needs.

It mixes two resources to make what I needed.

Resources:
1. List IP addresses in a range (Microsoft – https://gallery.technet.microsoft.com/scriptcenter/List-the-IP-addresses-in-a-60c5bb6b)
2. Check IP Addresses Available in Azure Virtual Network (IT Pro Windows – http://windowsitpro.com/azure/check-ip-addresses-available-azure-virtual-network)

function Get-IPrange
    {
        <# 
            .SYNOPSIS  
                Get the IP addresses in a range 
            .EXAMPLE 
                Get-IPrange -start 192.168.8.2 -end 192.168.8.20 
            .EXAMPLE 
                Get-IPrange -ip 192.168.8.2 -mask 255.255.255.0 
            .EXAMPLE 
                Get-IPrange -ip 192.168.8.3 -cidr 24 
        #> 
 
        param 
        ( 
          [string]$start, 
          [string]$end, 
          [string]$ip, 
          [string]$mask, 
          [int]$cidr 
        ) 
 
        function IP-toINT64 () { 
          param ($ip) 
 
          $octets = $ip.split(".") 
          return [int64]([int64]$octets[0]*16777216 +[int64]$octets[1]*65536 +[int64]$octets[2]*256 +[int64]$octets[3]) 
        } 
 
        function INT64-toIP() { 
          param ([int64]$int) 

          return (([math]::truncate($int/16777216)).tostring()+"."+([math]::truncate(($int%16777216)/65536)).tostring()+"."+([math]::truncate(($int%65536)/256)).tostring()+"."+([math]::truncate($int%256)).tostring() )
        } 
 
        if ($ip) {$ipaddr = [Net.IPAddress]::Parse($ip)} 
        if ($cidr) {$maskaddr = [Net.IPAddress]::Parse((INT64-toIP -int ([convert]::ToInt64(("1"*$cidr+"0"*(32-$cidr)),2)))) } 
        if ($mask) {$maskaddr = [Net.IPAddress]::Parse($mask)} 
        if ($ip) {$networkaddr = new-object net.ipaddress ($maskaddr.address -band $ipaddr.address)} 
        if ($ip) {$broadcastaddr = new-object net.ipaddress (([system.net.ipaddress]::parse("255.255.255.255").address -bxor $maskaddr.address -bor $networkaddr.address))} 
 
        if ($ip) { 
          $startaddr = IP-toINT64 -ip $networkaddr.ipaddresstostring 
          $endaddr = IP-toINT64 -ip $broadcastaddr.ipaddresstostring 
        } else { 
          $startaddr = IP-toINT64 -ip $start 
          $endaddr = IP-toINT64 -ip $end 
        } 
 
        for ($i = $startaddr; $i -le $endaddr; $i++) 
        { 
          INT64-toIP -int $i 
        }
    }

function Get-FirstAvailableIP
    {
        <# 
            .SYNOPSIS  
                Get the first available IP address in a range 
            .EXAMPLE 
                Get-FirstAvailableIP vNetworkName 10.60.0.128 26
        #> 
 
        param 
        ( 
          [string]$networkName,
          [string]$networkIP,
          [int]$networkCIDR 
        ) 
        $setIP=0

        foreach ($ip in $(Get-IPrange -ip $networkIP -cidr $networkCIDR))
        {
            $Address = Test-AzureStaticVNetIP -VNetName $networkName -IPAddress $ip
            If ($Address.IsAvailable –eq $False)
                {
                    #Write-Host "$ip is not available" -ForegroundColor Red
                }
            else
                {
                    #Write-Host "$ip is available" -ForegroundColor Green
                    $setIP=$ip
                    break
                }
        }

        Write-Output $setIP
    }
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.