Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell Azure – Get First Available IP From IP Range

Posted on October 27, 2016 By David Kittell

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.

Related

Azure Code PowerShell

Post navigation

Previous post
Next post

Related Posts

Azure – CLI – Build Windows/UNIX VM Function (With or without public IP)

Posted on December 19, 2017

In the functions below the VM name is used in all of the pieces to build the VM to simplify documentation. Important Items: As I use a Firewall appliance in Azure I do not set Network Security Groups (NSG). I have a predefined virtual network (VN) and predefined network subnet…

Read More

PowerShell – Reset Colors

Posted on October 20, 2017

If you have ever played with the colors of your PowerShell window this will help you reset the colors to default > $psISE.Options SelectedScriptPaneState : Top ShowDefaultSnippets : True ShowToolBar : True ShowOutlining : True ShowLineNumbers : True TokenColors : {[Attribute, #FF00BFFF], [Command, #FF0000FF], [CommandArgument, #FF8A2BE2], [CommandParameter, #FF000080]…} ConsoleTokenColors :…

Read More

Change Text Case

Posted on February 22, 2013February 8, 2016

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Globalization; using System.Threading; namespace ChangeTextCase { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnCopy_Click(object sender, EventArgs e) { txtDestination.SelectAll(); txtDestination.Copy(); } private void btnChangeCase_Click(object sender, EventArgs e) {…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes
 

Loading Comments...
 

You must be logged in to post a comment.