Saturday, July 26, 2014

Azure Virtual Machine using Powershell – common operations

This is very common scenario I have observed, many times the
IT guys need to perform day to day tasks of Azure with the help of Powershell
command and he finds dozens of ways to do it. In this post I am providing the
list of all common and basic operation of Azure Virtual Machine using
Powershell. If I have missed any operation on Azure virtual machine with
powershell, then feel free to comment.

To perform any task in powershell there are numerous ways, I
will be showing the easiest way to achieve it. As expected this post is also
going to be big one!!!

Setting
up Azure Account on Powershell window

This is mandatory step. Without this setup you cannot perform
a single operation using Azure powershell. There are two ways to set up your
credentials in Azure Powershell. One is to use Azure publish settings files and
importing in powershell or you can directly login credentials by using
Add-AzureAccount. Here I am depicting the second way –

#add azure account to powershell current
session

Set-ExecutionPolicy RemoteSigned

Add-AzureAccount

Select-AzureSubscription -SubscriptionName"Your Subscription name"

Setting
up Storage Account

Azure Virtual machine disks and images are stored in Azure
Storage therefore we need to have storage account. If you have an existing
storage account then definitely you can use that or create new. Following
commands specify how you can create a new storage account and also depicts how
can you turn ON or OFF the geo replication for account. I don’t wanted to have
geo replication therefore to save on cost I am making locally redundant by
specifying

-GeoReplicationEnabled$false

Here are the complete commands –

#this section is required only if you
don't have storage account available, if you have one ready then use that.#set storage account details$location = "West Europe" #this can be any location of
your choice$storageAccountName = "YourStorageAccountName"

#set geo replication to locally
redundant. If you wish to setup to Geo redundant then set below parameter to
$true

This lists down all the VM images available. I will be taking
the entire article with respect to Azure virtual Machine SQL Server with
powershell therefore I searched for SQL Server family as shown below –

I used following command to list images based on published
date in descending order so that I can select the latest image available in the
location and here is the result screenshot.

2.If you are specifying cloud service name which is
not created in subscription then specify location parameter as well – New-AzureQuickVM
-ImageName $latestImage.ImageName -Windows -Name $vmName -ServiceName "yourNonexistingcloudServiceName"
-AdminUsername $adminLogin -Password $adminPasswd -Location $location
-InstanceSize $instanceSize

Download
Azure VM RDP using Powershell

Following command can be used to download he Azure VM RDP file
to your desktop using powershell –

The above command creates a data disk and attaches to azure
virtual machine. In above command you can change the size, label as per your
choice.

In Above command LUN stands for Logical Unit Number, and it
can vary from 1 to 15 only which means any virtual machine in azure can have
maximum 15 data disks attached. Also LUN for each disk has to be unique.

Caution –
If
you try to use the disk that was already attached to any running VM then you
need to first detach the disk.You can
use below powershell option to detach the disk – section - Detach data disk from Azure virtual machine. After this, on
management portal you may observe that disk is not attached to any of the VM
and “Attached To” column is empty.

After this if you run above command to attach the disk from
blob storage then you may receive error as-The VHD
is already registered with image repository as the resource with ID.

To resolve this error you will need to delete the disk but
retain the associated vhd. For this you can use below powershell command to
delete the disk but to retain the associated vhd –

When you invoke above command, the attached disk is only
detached from the azure virtual machine and it is not deleted from storage.
Therefore it will be your responsibility to get it removed from azure storage
location. Following screenshot shows the same –

1.Using below command your virtual machine will stop
however it will not go in Deallocated state. The state of virtual machine will
be “Stopped” and hence you will be charged. However, your provisioned IP will
not be lost.

Followers

About Me

I am Kunal Chandratre. Working as Cloud Solution Architect @Microsoft. My speciality is Microsoft Azure Cloud platform.
Awarded as Most Valuable Professional (MVP) in Microsoft Azure for consecutive 3 years. Passionate speaker, trainer...In free time (which I don't get usually)I write blogs and answers the forum questions. I was doing it just for timespass but now I have got addicted to blogging...Apart from work, I do variety of things which I can't tell here:).. I am trekker, singer, actor, painter, f1 racer, super hero in my dreams.. ...and now trying my luck with technologies...Keep posting...

Visitors

Disclaimer:

The information shared in this blog is the result of my personal experience with various technology platforms. In no way it represents the company I work for.
The information provided here is "AS IS" with no warranties, and confers no rights. This blog does not represent the thoughts, intentions, plans or strategies of my current employer or past empolyers or any other forums or community I belong to. It is fully my own opinion. Inappropriate comments will be deleted at the authors discretion. All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.I have full rights to edit/modify/delete any content of this blog without any prior notice to public/followers/RSS readers of this blog.