Adding a Public IP to an Existing Azure ARM VM

If you are not running a jump host in your environment I find from time to time that I need to add a Public IP to a NIC and connect to my virtual machine.

PowerShell is by far the easiest way to complete this task. The small script below outlines how to do this.

# New-AzurePublicRmIAddress creates the new IP - Run this first. 

new-azurermpublicIPAddress -Name testip -ResourceGroupName wpbackup -AllocationMethod Static -Location "Southeast Asia"

# Set the variables but getting the properties you need 
$nic = Get-AzurermNetworkInterface -ResourceGroupName Nameof ResourceGroup -Name NameofNIC
$pip = Get-AzurermPublicIPAddress -ResourceGroupName wpbackup -Name testip
$nic.IPConfigurations[0].PublicIPAddress=$pip

# Finally set the IP address against the NIC
Set-AzureRmNetworkInterface -NetworkInterface $nic

Disclaimer:  Please note although I work for Microsoft the information provided here does not represent an official Microsoft position and is provided as is.

(Visited 1,764 times, 83 visits today)

2 thoughts on “Adding a Public IP to an Existing Azure ARM VM

  1. Although this adds the PIP to the network interface its not adding it to the VM.

    Any ideas how you would enumerate the NIC attached to a particular VM and then add the PIP to that Nic?

    1. If the NIC is a property of the VM adding the Public IP property to it should make it available. It should be the same process in the portal. It is however a while since I have reviewed.

Leave a Reply

Your email address will not be published. Required fields are marked *