Azure ARM Reserved Public Ip Addresses via Powershell

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.

#Gordon.byers@microsoft.com
#Powershell script is provided as-is and without any warranty of any kind

function Grab-AzurePublicIps  
{  
    param(
        [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")]
        [string]$pipNamePrefix,

        [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"
    [array]$publicIps={}

    for($i=1
        $i -le $NumberOfIps
        $i++)
    {
        $pipName = $pipNamePrefix + $i

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

        $publicIps += $pip
    }

    return $publicIps
}

#Login to account if needed
#Login-AzureRmAccount

$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;

My_IPBag_1 13.79.152.42 Succeeded
My_IPBag_2 13.79.159.10 Succeeded
My_IPBag_3 52.169.176.221 Succeeded
My_IPBag_4 13.79.156.94 Succeeded
My_IPBag_5 13.79.157.129 Succeeded
My_IPBag_6 13.79.153.147 Succeeded
My_IPBag_7 13.79.157.117 Succeeded
My_IPBag_8 13.79.153.183 Succeeded
My_IPBag_9 13.79.154.248 Succeeded
My_IPBag_10 13.79.157.10 Succeeded
My_IPBag_11 13.79.155.174 Succeeded

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.

New-AzureRmPublicIpAddress : Cannot create more than 20 public IP addresses with static allocation method for this subscription in this region.