Azure : Powershell Script to fix an Azure virtual machine IP

Hi All,

When provisioning an Azure virtual machine connected to an Azure network, the machine receives an IP address within the network address space field. Unfortunately, the IP provided is dynamic and provided via an Azure DHCP platform.But, we need to keep in mind some aspects:

  1. If you restart your VM, the IP will remain the same
  2. If you shutdown your VM from the operating system, the machine will be stopped. And when you start back your machine, the VM will will remain the same too.
  3. If you shutdown your VM from the Azure portal or via an Azure powershell command or API, the machine will be stopped. When you start back your VM, the machine may receive a new IP address
  4. If an Azure host or hardware failure occurs, the Azure controller will try to start the machine on a healthy host. The VM may receive a new IP address

The question is why ?

The answer can simply be resumed on two sentences:

  1. The IP address is dynamic, so don’t be surprised that one day, you figure-out that the IP has changed 🙂
  2. There are two types of VM shutdown :
    1. Shutdown without de-provisioning: When you shutdown your VM from the OS itself, the VM is shuttled down but Azure will keep it resources ready (IP, VIP…). The bad news is that Azure will keep billing this VM even if it’s really stopped and dos not use compute resources.
    2. Shutdown with de-provisioning : If you shutdown your VM for the Azure portal, Powershell command or using Azure API, the machine will be stopped and de-provisoned (De-allocated). All its non-static resources will be lost, certainly i’m taling about IP and VIP

Look her for more information about this interesting topic : HERE

To avoid loosing your IP configuration, make your life easier by fixing the IP addresses of your VM.

Until this time, fixing a VM IP address can only be done using powershell. I created this simple Powershell script that can be used to fix your IP address.

Steps

  • Download and install  the Azure Powershell modules HERE
  • Copy the script to a local location (Example : C:\Scripts)
  • Run an elevated Powershell window
  • change the powershell script execution policy to unrestricted (Set-ExecutionPolicy Unrestricted)

POwershellExecutionpolicy

  • You should to provide the following information

Script Parameters

  • Browse the folder where the script resides and run it using the following syntax
    • .\SetAzureVMStaticIP.ps1 -SubscriptionName ‘AAAA’ -VMName  ‘BBBB’ -ServiceName ‘CCCC’ -VNETName  ‘DDDD’ -IP  ‘EEEE’

Example

.\SetAzureVMStaticIP.ps1 -SubscriptionName ‘BuildWindows SAMIR FARHAT’ -VMName  ‘Renault4L01’ -ServiceName ‘Renault4L01’ -VNETName  ‘VNET01’ -IP  ‘192.192.1.100’

NB

  • This script will only work on VMs with one single NIC. I will soon post another script that support fixing IP addresses for VMs with multiple NICs
  • Azure will restart your machine to apply this modification, so be aware of it

Download HERE

Advertisements

2 thoughts on “Azure : Powershell Script to fix an Azure virtual machine IP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s