Provision VM using Azure PowerShell

Prepare the environment

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

Command: Set-ExecutionPolicy -ExecutionPolicy Unrestricted


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

Command: Install-Module AzureRM

Import-module AzureRm

Login-AzureRmaccount

Create 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 the name of the resource group and South India is the azure data center region.

we can see in the output resource group that has been 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, the storage account name is prodvmsa and it resides in an earlier created resource group and azure region.

A storage account has been created and is 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.

A virtual network has been created with a defined IP address and is ready to use for VM.

4. Network Interface

The network interface is a virtual NIC that 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, the 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, and 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 we need to assign user credentials to log in to the operating system and so on. Use the 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 been created successfully and I hope it is informative, in the next post we will discuss how to manage VM using Azure PowerShell. Also see, how to backup Azure VM