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#set storage account details
$location = "West Europe"
#this can be any location of your choice
$storageAccountName = "YourStorageAccountName"
Set-AzureStorageAccount -StorageAccountName $storageAccountName
-GeoReplicationEnabled $false
#set newly created storage account for
current subscription
Set-Azuresubscription -SubscriptionName "YourSubscriptionName" -CurrentStorageAccountname
$storageAccountName
Setting
up Azure cloud Service (Optional)
Following command actually create a cloud service in your
subscription, if you any cloud service existing then you can use the same to
deploy your vm –
#create cloud service, if already present
then you can use that
$cloudServiceName = "YourCloudServiceName"
New-AzureService -ServiceName
$cloudServiceName -Location $locations
Selecting
latest published Azure Virtual Machine Image using powershell
First run the command –
Get-AzureVMImage
Get-AzureVMImage
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.
#retrieve the latest image of your choice
based on published date
$images = Get-AzureVMImage | where { $_.ImageFamily -eq "SQL Server 2012 SP1
Enterprise on Windows Server 2012" } | where { $_.Location.Split(";") -contains $location } | Sort-Object -Descending -Property
PublishedDate
$latestImage = $images[0]
Highlighted area shows the latest version available for SQL Server
as on today in West Europe region and it got selected correctly.
Set up
Credentials for Azure Virtual Machine using Powershell
In this step let’s setup the credentials for Azure virtual
machine. Firs declare the variables with appropriate values –
#set virtual machine variables
$vmName = "YourVMName"
$adminLogin = "YourUserName"
$adminPasswd = "YourPassword"
Create
Azure virtual Machine using Powershell
Let’s first define the VM specific variables as follows –
#set virtual machine variables
$vmName = "powershellvm"#should not be more that 13
characters as of now
$instanceSize = "Basic_A1"
#Possible values - #ExtraSmall,Small,Medium,Large,ExtraLarge,A5,A6,A7,A8,A9,Basic_A0,Basic_A1,Basic_A2,Basic_A3, Basic_A4
Below this point all commands uses variables declared above.
So in case you are not able to understand from where the particular variable
values come, do not hesitate to look above this point. J