Create Azure VM from Snapshot — PowerShell

The 3 overview steps to creating (or restoring) an Azure virtual machine (VM) from a stored snapshot of another VMs Operating System (OS) virtual hard disks (vhd).

  1. Create a Snapshot (already created for this exercise)
  2. Create Managed disks from Operating System (OS) and Data Disk Snapshots
  3. Create a VM from the Managed OS Disk and add data disk

This post shows how to create a VM in a new resource group, from disk snapshots. We will be attaching the new managed OS Disk to the VM with PowerShell while the VM is created. The data disk will be attached after the VM is created. Note in the diagram below, that after creating the managed disks, we will also need to add 4 more Azure resources for the new VM to be accessible:

  • VNet
  • Subnet
  • PIP
  • Network Interface

#1. Login to Azure

#2. Declare Variables

$SourceResourceGroup = <YourSourceResourceGroupName>
$Location = <DestinationResourceGroupLocation>
$SnapshotName = <YourOSSnapShotName>
$ManagedDiskName = <OSManagedDiskName>
$DataDiskSnapshotName = <YourDataDiskSnapshotName>
$ManagedDataDiskName = <YourManagedDataDiskName>
$DestinationResourceGroup = <YOurDestinationResourceGroupName>
$StorageType = 'StandardLRS'
$VnetName = <YourVNetName>
$VnetAddressPrefix = <YourAddressChoice>
$SubnetName = <YourSubnetName>
$NewVmName = <NewVMName>
$NewVmSize = 'Standard_D2_v2_Promo'
$NewVm_pip = <YourNewVM_PIP>
$NewVm_nic = <YourVM_NIC>

#3. Create the Destination Resource Group and add a Virtual Network

New-AzureRmResourceGroup -DemoVM-RG -Location $Location
$VirtualNetwork = New-AzureRMVirtualNetwork -ResourceGroupName $DestinationResourceGroup -Location $Location -Name $VnetName -AddressPrefix $VnetAddressPrefix
$VirtualNetwork = Get-AzureRMVirtualNetwork -Name $VnetName -ResourceGroupName $DestinationResourceGroup
$SubNetConfig = Add-AzureRMVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $VnetAddressPrefix -VirtualNetwork $VirtualNetwork

#4. Create a Managed Disk (OS) from Snapshot

$Snapshot = Get-AzureRmSnapshot -ResourceGroupName $SourceResourceGroup -SnapshotName $SnapshotName$NewOSDiskConfig = New-AzureRmDiskConfig -AccountType $StorageType -Location $Snapshot.Location -SourceResourceId $Snapshot.Id -CreateOption Copy
$NewOSDisk = New-AzureRmDisk -Disk $NewOSDiskConfig -ResourceGroupName $DestinationResourceGroup -DiskName $ManagedDiskName

#5. Create a Managed Disk (Data) from Snapshot

$DataSnapshot = Get-AzureRmSnapshot -ResourceGroupName $SourceResourceGroup -SnapshotName $DataDiskSnapshotName$NewDataDiskConfig = New-AzureRmDiskConfig -AccountType $StorageType -Location $DataSnapshot.location -SourceResourceId $DataSnapshot.Id -CreateOption Copy
$NewDataDisk = New-AzureRmDisk -Disk $NewDataDiskConfig -ResourceGroupName $DestinationResourceGroup -DiskName $ManagedDataDiskName

#6. After creating the PIP & Network Interface for the VM, create a new VM from the Managed OS Disk:

$VNet = Get-AzureRmVirtualNetwork -Name $VnetName -ResourceGroupName $DestinationResourceGroup$publicIP = New-AzureRmPublicIpAddress -Name $NewVm_pip -ResourceGroupName $DestinationResourceGroup -Location $Location -AllocationMethod Dynamic$subnetid = (Get-azurermvirtualNetworkSubnetConfig -Name $SubnetName -VirtualNetwork $vnet).Id$NIC = New-AzureRmNetworkInterface -Name $NewVM_nic -ResourceGroupName $DestinationResourceGroup -Location centralus -SubnetId $subnetid -PublicIpAddressId $publicIP.Id$VirtualMachine = New-AzureRmVMConfig -VMName $NewVMName -VMSize $NewVMSize 
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $NewOSDisk.Id -CreateOption Attach -Windows
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id
$VirtualMachine = Set-AzureRmVMBootDiagnostics -VM $VirtualMachine -Disable
New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $DestinationResourceGroup -Location $Location

#7 Start the VM

Start-AzureRmVM -Name $NewVmName -ResourceGroupName $DestinationResourceGroup

#8 Attach the new managed Data Disk & update the VM:

$VirtualMachine = Get-AzureRmVM -ResourceGroupName $DestinationResourceGroup -Name $NewVmName
$ManagedDiskId = Get-AzureRmDisk -ResourceGroupName $DestinationResourceGroup -DiskName $ManagedDataDiskName
Add-AzureRmVMDataDisk -VM $VirtualMachine -Name $ManagedDataDiskName -ManagedDiskId $ManagedDiskId.IdUpdate-AzureRmVM -ResourceGroupName $DestinationResourceGroup -VM $VirtualMachine

The Azure portal is showing both the os and data disks attached on the new VM:

Originally published at https://arlanblogs.alvarnet.com on June 15, 2018.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store