Monday, 11 May 2015

Assigning Static ip address to VM through PowerShell in Azure

We can assign static ip address to VM in Azure through PowerShell and new management portal which was not possible in older Azure management portal.

Static IP address doesn't mean that TCP/IP configuration inside a VM will have "static ip" as ticked mark (no it will be dynamic in TCP/IP configuration), but azure will make sure the VM gets same IP address always.

In this article, we will have a look on how to assign static ip address to VM through PowerShell. Before assigning static ip address to VM, we should first create the Virtual Network and subnet so that we can assign one ip address from Virtual Network Subnet to VM.

Following things to be added while deploying VM via PowerShell so as to assign static ip address to VM from Network Subnet under Virtual Network.

$vnetName = "SQUONS-NETWORK"     # Name of Virtual Network
$subnet = "SQUONS-SUBNET"               # Name of Subnet created under Virtual Network
$staticIP = "192.168.1.10"                         # Static ip to be assigned to VM
New-AzureVMConfig -Name $vmName `
                                      -InstanceSize $size `
                                      -ImageName $imageName |

Add-AzureProvisioningConfig -Windows `
                                                    -AdminUsername $adminUser `
                                                    -Password $password |

Set-AzureStaticVNetIP –IPAddress $staticIP |
               Set-AzureSubnet –SubnetNames $subnet |
                          New-AzureVM -ServiceName $serviceName `
                                                    -Location $location `
                                                   -VNETName $vnetName

As we are assigning static ip address through PowerShell, I have already created Virtual Network in Azure management portal named as “SQUONS-NETWORK” and subnet named as “SQUONS-SUBNET”.



Now, while deploying the VM via PowerShell you get the option to assign static ip address as depicted below:

$adminUser = "Shakeel"
$password = "###########"
$serviceName = "squons"                # ServiceName means CloudService name e.g. “squons”
$location = "West US"                     # In which location you want to deploy your VM e.g. "West US"
$size = "Medium"                                       # Size/Tier name e.g. “Small/Medium/Large” etc
$vmName = "SQUONS-VNET-VM2"                # Name or Virtual Machine e.g. “SQUONS-VM1” 
$imageFamily = "Windows Server 2012 R2 Datacenter"
$vnetName = "SQUONS-NETWORK"                       # Name of Virtual Network
$subnet = "SQUONS-SUBNET"                          # Name of Subnet created under Virtual Network
$staticIP = "192.168.1.10"                         # Static ip to be assigned to VM
$imageName = Get-AzureVMImage |
                                                             where { $_.ImageFamily -eq $imageFamily } |
                                                             sort PublishedDate -Descending |
                                                             select -ExpandProperty ImageName -First 1

New-AzureVMConfig -Name $vmName `
                                      -InstanceSize $size `
                                      -ImageName $imageName |

Add-AzureProvisioningConfig -Windows `
                                                    -AdminUsername $adminUser `
                                                    -Password $password |

Set-AzureStaticVNetIP –IPAddress $staticIP |
 Set-AzureSubnet –SubnetNames $subnet |
  New-AzureVM -ServiceName $serviceName `
                            -Location $location `
                            -VNETName $vnetName



To verify the properties of deployed VM, use the following syntax:

Get-AzureVM –NAME “VM Name” –ServiceName “Cloud Service Name”





 

No comments:

Post a Comment