Azure ARM Reserved Public Ip Addresses via Powershell

2016, Mar 31    

I’ve had the situation where the full allocation of IP addresses are needed up front for firewall configuration. This isn’t so bad when you only need a few, but if you’re going for your full allocation of 20 (or more if you’ve spoken to Microsoft Support) then it can get a little tedious using the Azure Portal UI.
#Powershell script is provided as-is and without any warranty of any kind

function Grab-AzurePublicIps  
        [Parameter(Mandatory=$true, HelpMessage="Enter Azure Region location where the IP addresses will be allocated")]
        [string]$location = "Central US",

        [Parameter(Mandatory=$true, HelpMessage="Enter the name of the Resource Group where the IP addresses will be allocated")]
        [string]$rgName = "IpBlock" ,

        [Parameter(Mandatory=$true, HelpMessage="Enter the first part of your IP naming convention, names will be suffixed with an incremented number")]

        [Parameter(Mandatory=$true, HelpMessage="Enter the number of IP addresses to obtain")]
        [int]$NumberOfIps = 5

    #Create Resource Group
    $rgroup = Get-AzureRmResourceGroup -Name $rgName -Location $location -ErrorAction SilentlyContinue
    if($rgroup -eq $null) { 
        Write-Host "Creating resource group $rgName as it does not exist" 
        New-AzureRmResourceGroup -Name $rgName -Location $location

    Write-Host "Creating $NumberOfIps IP addresses"

        $i -le $NumberOfIps
        $pipName = $pipNamePrefix + $i

        $pip = New-AzureRmPublicIpAddress -Name $pipName -ResourceGroupName $rgName -AllocationMethod Static -Location $location

        $publicIps += $pip

    return $publicIps

#Login to account if needed

$pips = Grab-AzurePublicIps -location "North Europe" -rgName "My_IPBag" -pipNamePrefix "My_IPBag_" -NumberOfIps 5
$pips | select name, ipaddress, provisioningstate | ft

The typical output from this should be something like;
<br /> My_IPBag_1 Succeeded<br /> My_IPBag_2 Succeeded<br /> My_IPBag_3 Succeeded<br /> My_IPBag_4 Succeeded<br /> My_IPBag_5 Succeeded<br /> My_IPBag_6 Succeeded<br /> My_IPBag_7 Succeeded<br /> My_IPBag_8 Succeeded<br /> My_IPBag_9 Succeeded<br /> My_IPBag_10 Succeeded<br /> My_IPBag_11 Succeeded<br />
Note that the assigned IP addresses are not in a continuous range 🙂

If you do try to obtain more than 20 Public IP Addresses in your subscription then you’ll get this friendly error message.
<br /> New-AzureRmPublicIpAddress : Cannot create more than 20 public IP addresses with static allocation method for this subscription in this region.<br />