Azure User Group Nepal

Learn and understand the cloud technology using Microsoft Azure platform to adopt it

Provision VM using Azure PowerShell

Author: Sakaldeep

July 17, 2017

Prepare the environment

Open PowerShell ISE as an administrator and run this command below command to set script execution policy.

Command: Set-ExecutionPolicy -ExecutionPolicy Unrestricted


Run below command to install AzureRM module if it has not installed yet. It will install all the required package from Microsoft gallery, the Internet connection is required to download the package.

Command: Install-Module AzureRM

Import-module AzureRm

Login-AzureRmaccount

Create an Azure VM Prerequisites.

1. Resource Group

A container that holds related resources for an Azure solution. The resource group can include all the resources for the solution, or only those resources that we want to manage as a group.

Command:

$resourceGroup=”Production-VM”
$location=”South India”
New-AzureRmResourceGroup -Name $resourceGroup -location $location

Here, Production-VM is a name of the resource group and South India is azure data center region.

we can see in the output resource group has created.

2. Storage Account

The storage account is an account for storing data in Azure Storage. we need this account to store VHD files.

Command:

$storageAccountName= "prodvmsa"
New-AzureRmStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroup -Type Standard_LRS -Location $location

Here, storage account name is prodvmsa and it resides in an earlier created resource group and azure region.

Storage account has created and ready to use to store VHDs.

3. Virtual Network

Azure Virtual Network service enables you to securely connect Azure resources to each other with virtual networks (VNets). A VNet is a representation of your own network in the cloud.

Command:

$vnetName="prodvm-net"
$subnet= New-AzureRmVirtualNetworkSubnetConfig -Name prodvmsubnet -AddressPrefix 10.0.1.0/24
$vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $resourceGroup -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $subnet

VNet name is prodvm-net which has network id 10.0.0.16 and it has a subnet having network id 10.0.1.0/24.

Virtual network has created with a defined IP address and ready to use for VM.

4. Network Interface

The network interface is a virtual NIC which will be attached to VM

Command:

$nicName ="Prodvm-nic"
$pip = New-AzureRmPublicIpAddress -Name $nicName -ResourceGroupName $resourceGroup -Location $location -AllocationMethod Dynamic
$nic =New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $resourceGroup -Location $location -SubnetId $vnet.Subnets[0].id -PublicIpAddressId $pip.Id

Here, NIC name is Prodvm-nic and it will be assigned public IP dynamically.

Creating VM

All the required resources are ready to create VM such as resource group, storage account, virtual network, network interface.

Command:

$vmName="Win-Vm1"
$vm= New-AzureRmVMConfig -VMName $vmName -VMSize "Basic_A1"$

VM has created having name win-Vm1 and VM size is Basic_A1. Now need to assign user credentials to log in, operating system and so on. Use below command to perform the task.

Command:

$cred=Get-Credential -Message "Admin Credentials"
$vm=Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $vmName -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
$vm=Set-AzureRmVMSourceImage -VM $vm -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-datacenter" -Version "latest"
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
$diskName="os-disk"
$storrageAcc=Get-AzureRmStorageAccount -ResourceGroupName $resourceGroup -Name $storageAccountName
$osDiskUri=$storrageAcc.PrimaryEndpoints.Blob.ToString()+"vhds/"+$diskName+".vhd"
$vm=Set-AzureRmVMOSDisk -VM $vm -Name $diskName -VhdUri $osDiskUri -CreateOption FromImage
New-AzureRmVM -ResourceGroupName $resourceGroup -Location $location -VM $vm

VM has created successfully and I hope it is informative, in next post we will discuss how to manage VM using Azure PowerShell. Also see, how to backup Azure VM

Total visits : 36