Configure private IP addresses for a virtual machine (Classic) using PowerShell

Your IaaS virtual machines (VMs) and PaaS role instances in a virtual network automatically receive a private IP address from a range that you specify, based on the subnet they are connected to. That address is retained by the VMs and role instances, until they are decommissioned. You decommission a VM or role instance by stopping it from PowerShell, the Azure CLI, or the Azure portal. In those cases, once the VM or role instance starts again, it will receive an available IP address from the Azure infrastructure, which might not be the same it previously had. If you shut down the VM or role instance from the guest operating system, it retains the IP address it had.

In certain cases, you want a VM or role instance to have a static IP address, for example, if your VM is going to run DNS or will be a domain controller. You can do so by setting a static private IP address.

Important

Before you work with Azure resources, it's important to understand that Azure currently has two deployment models: Azure Resource Manager and classic. Make sure you understand deployment models and tools before you work with any Azure resource. You can view the documentation for different tools by clicking the tabs at the top of this article.

This article covers the classic deployment model. You can also manage a static private IP address in the Resource Manager deployment model.

Scenario

To better illustrate how to configure a static IP address for a VM, this document will use the scenario below.

VNet scenario

In this scenario you will create a VM named DNS01 in the FrontEnd subnet, and set it to use a static IP address of 192.168.1.101.

The sample PowerShell commands below expect a simple environment already created. If you want to run the commands as they are displayed in this document, first build the test environment described in Create a VNet.

How to verify if a specific IP address is available

To verify if the IP address 192.168.1.101 is available in a VNet named TestVNet, run the following PowerShell command and verify the value for IsAvailable:

Test-AzureStaticVNetIP –VNetName TestVNet –IPAddress 192.168.1.101 

Expected output:

IsAvailable          : True
AvailableAddresses   : {}
OperationDescription : Test-AzureStaticVNetIP
OperationId          : fd3097e1-5f4b-9cac-8afa-bba1e3492609
OperationStatus      : Succeeded

How to specify a static private IP address when creating a VM

The PowerShell script below creates a new cloud service named TestService, then retrieves an image from Azure, creates a VM named DNS01 in the new cloud service using the retrieved image, sets the VM to be in a subnet named FrontEnd, and sets 192.168.1.7 as a static private IP address for the VM:

New-AzureService -ServiceName TestService -Location "Central US"
$image = Get-AzureVMImage | where {$_.ImageName -like "*RightImage-Windows-2012R2-x64*"}
New-AzureVMConfig -Name DNS01 -InstanceSize Small -ImageName $image.ImageName |
  Add-AzureProvisioningConfig -Windows -AdminUsername adminuser -Password MyP@ssw0rd!! |
  Set-AzureSubnet –SubnetNames FrontEnd |
  Set-AzureStaticVNetIP -IPAddress 192.168.1.7 |
  New-AzureVM -ServiceName TestService –VNetName TestVNet

Expected output:

WARNING: No deployment found in service: 'TestService'.
OperationDescription OperationId                          OperationStatus
-------------------- -----------                          ---------------
New-AzureService     fcf705f1-d902-011c-95c7-b690735e7412 Succeeded      
New-AzureVM          3b99a86d-84f8-04e5-888e-b6fc3c73c4b9 Succeeded  

How to retrieve static private IP address information for a VM

To view the static private IP address information for the VM created with the script above, run the following PowerShell command and observe the values for IpAddress:

Get-AzureVM -Name DNS01 -ServiceName TestService

Expected output:

DeploymentName              : TestService
Name                        : DNS01
Label                       : 
VM                          : Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVM
InstanceStatus              : Provisioning
IpAddress                   : 192.168.1.7
InstanceStateDetails        : Windows is preparing your computer for first use...
PowerState                  : Started
InstanceErrorCode           : 
InstanceFaultDomain         : 0
InstanceName                : DNS01
InstanceUpgradeDomain       : 0
InstanceSize                : Small
HostName                    : rsR2-797
AvailabilitySetName         : 
DNSName                     : http://testservice000.cloudapp.net/
Status                      : Provisioning
GuestAgentStatus            : Microsoft.WindowsAzure.Commands.ServiceManagement.Model.GuestAgentStatus
ResourceExtensionStatusList : {Microsoft.Compute.BGInfo}
PublicIPAddress             : 
PublicIPName                : 
NetworkInterfaces           : {}
ServiceName                 : TestService
OperationDescription        : Get-AzureVM
OperationId                 : 34c1560a62f0901ab75cde4fed8e8bd1
OperationStatus             : OK

How to remove a static private IP address from a VM

To remove the static private IP address added to the VM in the script above, run the following PowerShell command:

Get-AzureVM -ServiceName TestService -Name DNS01 |
  Remove-AzureStaticVNetIP |
  Update-AzureVM

Expected output:

OperationDescription OperationId                          OperationStatus
-------------------- -----------                          ---------------
Update-AzureVM       052fa6f6-1483-0ede-a7bf-14f91f805483 Succeeded

How to add a static private IP address to an existing VM

To add a static private IP address to the VM created using the script above, runt he following command:

Get-AzureVM -ServiceName TestService -Name DNS01 |
  Set-AzureStaticVNetIP -IPAddress 192.168.1.7 |
  Update-AzureVM

Expected output:

OperationDescription OperationId                          OperationStatus
-------------------- -----------                          ---------------
Update-AzureVM       77d8cae2-87e6-0ead-9738-7c7dae9810cb Succeeded 

Next steps