Today we needed to assign a static public IP to an Azure VM (referred to in documentation as Reserved). Unfortunately it seems this is best done before the VM is setup so I had to remove my old VM and start again – for me this wasn’t too big a deal but it’s something to bear in mind for the future.
Note Public IPs are chargeable in Azure – first 5 are free after that they are charged to 3 Euros a month. Pricing info can be found: http://azure.microsoft.com/en-us/pricing/details/ip-addresses/.
So first things first you need to connect via Powershell to Azure (you can follow my previous blog here: http://support.risualblogs.com/blog/2015/02/27/setting-a-static-ip-for-a-azure-vm/).
Firstly we need get a reserved IP:
New-AzureReservedIP -ReservedIPName “<NAME>” -Label “<LABEL>” -Location “North Europe” (or appropriate location).
You will get something like the below if successful:
To get info on your newly allocated IP you can run Get-AzureReservedIP.
Next we need to set the storage location for the VM you are going to create (you can use the –MediaLocation switch on the New-AzureVM command but I prefer doing it this way).
Get-AzureSubscription – this will give you some details and most importantly the SubscriptionName which you will need.
Get-AzureStorageAccount – Depending how many you have configured this will list out your storage services, you need to find the “CurrentStorageAccountName” of the service you wish to assign.
Finally you need to set the subscription to use the storage account:
Set-AzureSubscription –SubscriptionName “SubscriptionName” –CurrentStorageAccountName “StorageAccountName”
Next we need to find out which image you want to use – I just used the stock images from the gallery, to get just Windows images you need to run the following:
$images = get-azurevmimage | where os -like “*Windows*” | select ImageName
This will spit out a very long list! I put the result out to a text file and then counted the lines to see which image it was I needed:
$images | out-file c:images.txt
I wanted “a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-Datacenter-201502.01-en.us-127GB.vhd” which was line 113 – remember though you need to add 1 because arrays start at 0. So to get the image name I want I need to call $images.ImageName.
We will go line by line but all these should be run together (as shown at the end):
New-AzureVMConfig –Name “HOSTNAME” –InstanceSize “Small” –ImageName $images.ImageName
-Name – name of the Virtual Machine – becareful here there is no way to change this once the VM is up and running.
-InstanceSize – this relates to the variety of resources you can assign the VM – remember the more resource the more expensive it is – these are all listed here: https://msdn.microsoft.com/library/azure/dn197896.aspx
-ImageName – as discussed before this is the image to use for the VM.
Add-AzureProvisioningConfig –Windows –AdminUsername “LocalAdminUsername” –Password “Password”
This one tells it what OS and sets up the local admin username and password.
Next as the machine I am creating is going to be in a different subnet I need to set this:
Set-AzureSubnet -SubnetNames “SubnetRequired”
-SubnetName – you can get this from the portal under networks.
New-AzureVM –ServiceName “NameOfService” -VNetName “NetworkName” –ReservedIPName “NameOfReservedIP” –Location “North Europe”
-ServiceName – if you aren’t sure you can run Get-AzureService and pick out the name of the cloud you want the VM to be created in.
-VNetName – Network name – make sure your subnet that you specified earlier is part of that.
-ReservedIPName – this is the name we gave it earlier when we created it.
-Location – the region/location appropriate for your location.
Put them all together and it should then create your VM:
New-AzureVMConfig -Name “HOSTNAME” -InstanceSize “Small” -ImageName $images.ImageName | Add-AzureProvisioningConfig -Windows -AdminUsername “LocalAdminUsername” -Password “Password”| Set-AzureSubnet -SubnetNames “SubnetRequired” New-AzureVM –ServiceName “NameOfService” -VNetName “NetworkName” –ReservedIPName “NameOfReservedIP” –Location “North Europe”
You should get something like the below:
Hope it helps!