How to Assign a Private Static IP to an Azure VM

In most cases, you won’t need to specify a static internal IP address (DIP) for your virtual machine. VMs in a virtual network will automatically receive an internal IP address from a range that you specify. But in certain cases, specifying a static IP address for a particular VM makes sense. For example, if your VM is going to run DNS or will be a domain controller. Or, if you have a VM that you plan to stop/deprovision at some point, but want retain the DIP for the VM when you provision it again. A static DIP stays with the VM even through a stop/deprovision state. You can specify a static DIP by using PowerShell at the time you create the VM, or you can update an existing VM.

Here is an example script I use in my environment.

  • $IPaddress: You need to specify the static IP address to assign to your VM
  • $VNETName: You need to specify the Name of the virtual Network that the Static IP address belongs to
  • $VMName: The name of the VM to update
  • $ServiceName: The name of the cloud service to which the VM belongs
##############Variables#################
$IPaddress = "192.168.1.10"
$VNETName = "brv-vnet-192-168-1-0"
$VMName = "brv-ts02"
$ServiceName = "brv-ts02"
########################################
##############Main######################
if (Test-AzureStaticVNetIP -VNETName $VNETName -IPAddress $IPaddress)
{
                $VM = Get-AzureVM -ServiceName $ServiceName -Name $VMName
                Set-AzureStaticVNetIP -VM $VM -IPAddress $IPaddress | Update-AzureVM
                $VMStaticIPCheck = Get-AzureStaticVNetIP -VM $VM
                if ($VMStaticIPCheck -eq $null)
                {
                                "No Static IP address was assigned to " + $VMName + " VM"
                }
                else
                {
                                If ($VMStaticIPCheck.IPAddress -eq $IPaddress)
                                {
                                                $displaymessage = "The IP address " + $IPaddress + " was successfully assigned to " + $VMName
                                                write-host $displaymessage -foreground "Green"
                                }
                                else
                                {
                                                $displaymessage = "The IP address " + $IPaddress + " was not assigned to " + $VMName + ". The VM is using " + $VMStaticIPCheck.IPAddress + " as IP address"
                                                write-host $displaymessage -foreground "Red"
                                }
                }
}
else
{
                $displaymessage = $IPaddress + " IP address is not available."
                write-host $displaymessage -foreground "Red"
}

If you get the following error:

Test-AzureStaticVNetIP : Your Windows Azure credential in the Windows PowerShell session has expired. Please use Add-AzureAccount to login again.
At C:scriptstest.ps1:15 char:5
+ if (Test-AzureStaticVNetIP -VNETName $VNETName -IPAddress $IPaddress)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Test-AzureStaticVNetIP], AadAuthenticationCantRenewException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Utilities.Common.Authentication.AadAuthenticationCantRen
   ewException,Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.TestAzureStaticVNetIPCommand

Just use the command Add-AzureAccount to refresh your Azure credential.

Add-AzureAccount

After this command a window will popup asking for your mailaddress to logon to Microsoft Azure. (Sorry for the dutch screenshots)

Add-AzureAccount - mailaddress

Then enter your full credentials and hit logon.

Add-AzureAccount - Full Credentials

That’s it. If run the script and it is successful, it will return this:

(Picture lost)

 

Sources: http://msdn.microsoft.com/en-us/library/azure/dn630228.aspx and a TechNet wiki article.

Branko Vucinec

About Branko Vucinec

Hi! I'm Branko, a Systems Engineer focused on Microsoft technologies from the Netherlands. I enjoy helping organizations with the business and people opportunities and challenges surrounding tech.

Comments