Today, Azure managed disks has a long list of benefits that make them the ideal choice to use with your virtual machines. One of those benefits is the ability to take snapshots of your virtual disks. In this post, I will show you how to take snapshots of your managed disks and how to create a VM using these snapshots.

Requirements:

This tutorial assumes that you already have a Microsoft Azure account set up.

You have an existing virtual machine that uses managed disks. If you want to know how to convert Azure VM to use Managed Disks, see this link

Important: Snapshots are billed based on the used size.

Azure PowerShell Workaround

If you want to know how to install the PowerShell Azure module on your machine, check out this link.

The simplest way to get started is to sign in interactively at the command line.

Connect-AzAccount 1 2 3 Connect-AzAccount

This cmdlet will bring up a dialog box prompting you for your email address and password associated with your Azure account.

If you have more than one subscription associated with your mail account, you can choose the default subscription. To perform this task, we will use the following commands:

Get-AzSubscription Select-AzSubscription -Subscription "My Subscription" 1 2 3 4 Get-AzSubscription Select -AzSubscription -Subscription "My Subscription"

Create a snapshot

Set the variables

Here, we define the characteristics of our environment

$resourceGroupName = 'RG-DEMO-NE' $location = 'North Europe' $vmName = "VM-DEMO-NE" 1 2 3 4 5 $resourceGroupName = 'RG-DEMO-NE' $location = 'North Europe' $vmName = "VM-DEMO-NE"

Get the virtual machine

To get and store the virtual machine in a variable, use Get-AzVM with the following syntax.

$vm = Get-AzVM -Name $vmName ` -ResourceGroupName $resourceGroupName 1 2 3 4 $vm = Get-AzVM -Name $vmName ` -ResourceGroupName $resourceGroupName

Set the snapshot configuration

To set snapshot settings for an OS Disk, you should use New-AzSnapshotConfig cmdlet with the following syntax.

$snapshotConfig = New-AzSnapshotConfig -SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id ` -Location $location ` -CreateOption copy ` -SkuName Standard_LRS 1 2 3 4 5 6 $snapshotConfig = New-AzSnapshotConfig -SourceUri $vm . StorageProfile . OsDisk . ManagedDisk . Id ` -Location $location ` -CreateOption copy ` -SkuName Standard_LRS

Depending on the value established in the -SKU parameters you can store your snapshot in

Standard_LRS

Standard_ZRS

Premium_LRS

if instead, you want to set snapshot settings for a Data disk, you should use the following syntax.

$snapshotConfig = New-AzSnapshotConfig -SourceUri $vm.StorageProfile.DataDisks[0].ManagedDisk.Id ` -Location $location ` -CreateOption copy ` -SkuName Standard_LRS 1 2 3 4 5 6 $snapshotConfig = New-AzSnapshotConfig -SourceUri $vm . StorageProfile . DataDisks [ 0 ] . ManagedDisk . Id ` -Location $location ` -CreateOption copy ` -SkuName Standard_LRS

You must keep in mind the number of disks attached to your virtual machine to set the index. In my case, I only have a Data disk attached to the VM.

Take the snapshot

To take a snapshot, you should use the New-AzSnapshot cmdlet with the following syntax. In the following example, I set the name of the snapshots by adding the date to the name of the VM, but you can choose the name you want for your snapshots.,

$timestamp = Get-Date -Format yyMMddThhmmss $snapshotName = ($vmName+$timestamp) New-AzSnapshot -Snapshot $snapshotConfig ` -SnapshotName $snapshotName ` -ResourceGroupName $resourceGroupName 1 2 3 4 5 6 7 8 $timestamp = Get-Date -Format yyMMddThhmmss $snapshotName = ( $vmName + $timestamp ) New-AzSnapshot -Snapshot $snapshotConfig ` -SnapshotName $snapshotName ` -ResourceGroupName $resourceGroupName

Create an incremental snapshot

Microsoft has recently released the ability to take incremental snapshots of your managed disks. These types of snapshots include only all changes since the last snapshot. To use this type of snapshot follows the next steps.

You must first get the disk you have snapshots of and want to take an incremental snapshot. To do this you should use the cmdlet with the following syntax.

$disk = Get-AzDisk -DiskName $vm.StorageProfile.OsDisk.Name ` -ResourceGroupName $resourceGroupName 1 2 3 4 $disk = Get-AzDisk -DiskName $vm . StorageProfile . OsDisk . Name ` -ResourceGroupName $resourceGroupName

Set the snapshot configuration

To create an incremental snapshot, you should use the New-AzSnapShotConfig cmdlet with the new parameter -Incremental

$snapshotConfig=New-AzSnapshotConfig -SourceUri $disk.Id ` -Location $disk.Location ` -CreateOption Copy ` -Incremental 1 2 3 4 5 6 $snapshotConfig = New-AzSnapshotConfig -SourceUri $disk . Id ` -Location $disk . Location ` -CreateOption Copy ` -Incremental

Unlike regular snapshots, these types of snapshots are always stored on standard HDDs, regardless of the type of storage on the primary disks. Also, for increased reliability, they are stored in Zone Redundant Storage (ZRS) by default in regions that support ZRS.

Take the incremental snapshot

To take a snapshot, you should use the New-AzSnapshot cmdlet with the following syntax. In the following example, I set the name of the snapshots by adding the date to the name of the VM, but you can choose the name you want for your snapshots.

$timestamp = Get-Date -Format yyMMddThhmmss $snapshotName = ($vmName+$timestamp) New-AzSnapshot -Snapshot $snapshotConfig ` -SnapshotName $snapshotName ` -ResourceGroupName $resourceGroupName 1 2 3 4 5 6 7 8 $timestamp = Get-Date -Format yyMMddThhmmss $snapshotName = ( $vmName + $timestamp ) New-AzSnapshot -Snapshot $snapshotConfig ` -SnapshotName $snapshotName ` -ResourceGroupName $resourceGroupName

Create a VM from an existing snapshot

Here I will show you how to create a virtual machine from an existing snapshot taken from an OS disk.

Use the Get-AzSnapshot cmdlet, to list all available snapshots within the resource group is obtained.

Get-AzSnapshot -ResourceGroupName $resourceGroupName ` | Select-Object Name, DiskSizeGB, Incremental,ProvisioningState,UniqueId 1 2 3 4 Get-AzSnapshot -ResourceGroupName $resourceGroupName ` | Select-Object Name , DiskSizeGB , Incremental , ProvisioningState , UniqueId

Once the snapshot is identified, the snapshot needs to be converted to a virtual disk. To do this you should use the following commands.

$snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName ` -SnapshotName <SnapshotName> $diskConfig = New-AzDiskConfig -SkuName Standard_LRS ` -Location $location ` -CreateOption Copy ` -SourceResourceId $snapshot.Id $newDisk = New-AzDisk -DiskName "NewOSDisk" ` -Disk $diskConfig ` -ResourceGroupName $resourceGroupName 1 2 3 4 5 6 7 8 9 10 11 12 13 $snapshot = Get-AzSnapshot -ResourceGroupName $resourceGroupName ` -SnapshotName < SnapshotName > $diskConfig = New-AzDiskConfig -SkuName Standard_LRS ` -Location $location ` -CreateOption Copy ` -SourceResourceId $snapshot . Id $newDisk = New-AzDisk -DiskName "NewOSDisk" ` -Disk $diskConfig ` -ResourceGroupName $resourceGroupName

Then you should set the characteristics of the new VM. In my case, I use the original VM as a reference.

$vm = get-azvm -Name $vmName ` -ResourceGroupName $resourceGroupName 1 2 3 4 $vm = get -azvm -Name $vmName ` -ResourceGroupName $resourceGroupName

Here we set the size of the new virtual machine and disable boot diagnostics.

$newVM = New-AzVMConfig -VMName "VM-DEMO-COPY" ` -VMSize $VM.HardwareProfile.VmSize $newVM = Set-AzVMBootDiagnostic -VM $newVM ` -Disable 1 2 3 4 5 6 7 $newVM = New-AzVMConfig -VMName "VM-DEMO-COPY" ` -VMSize $VM . HardwareProfile . VmSize $newVM = Set -AzVMBootDiagnostic -VM $newVM ` -Disable

We use the Resource ID of the new managed disk to attach it to the virtual machine. In the last parameter, you can change the type of operating system to Linux if the operating system disk has Linux OS

$newVM = Set-AzVMOSDisk -VM $newVM ` -ManagedDiskId $newDisk.Id ` -CreateOption Attach ` -Windows 1 2 3 4 5 6 $newVM = Set -AzVMOSDisk -VM $newVM ` -ManagedDiskId $newDisk . Id ` -CreateOption Attach ` -Windows

select and store in the variable $vnet, the virtual network where the virtual machine will be hosted.

$vnet = Get-AzVirtualNetwork -Name "DEMO-VNET" ` -ResourceGroupName $resourceGroupName 1 2 3 4 $vnet = Get-AzVirtualNetwork -Name "DEMO-VNET" ` -ResourceGroupName $resourceGroupName

We create and add a network interface to the configuration of the new virtual machine.

$nic = New-AzNetworkInterface -Name ('NIC-'+$newVM.Name) ` -ResourceGroupName $resourceGroupName ` -Location $snapshot.Location ` -SubnetId $vnet.Subnets[0].Id $newVM = Add-AzVMNetworkInterface -VM $newVM ` -Id $nic.Id 1 2 3 4 5 6 7 8 9 $nic = New-AzNetworkInterface -Name ( 'NIC-' + $newVM . Name ) ` -ResourceGroupName $resourceGroupName ` -Location $snapshot . Location ` -SubnetId $vnet . Subnets [ 0 ] . Id $newVM = Add-AzVMNetworkInterface -VM $newVM ` -Id $nic . Id

and finally, we create a virtual machine with the established configuration.

New-AzVM -VM $newVM ` -ResourceGroupName $resourceGroupName ` -Location $snapshot.Location 1 2 3 4 5 New-AzVM -VM $newVM ` -ResourceGroupName $resourceGroupName ` -Location $snapshot . Location

Another possibility to access the data stored in the snapshots is to convert the snapshots to virtual hard disks and then mount them as data disks in another VM.

Thanks for reading my post. I hope you find it useful.

If you want to know more about Azure Managed Disks, check out this link:https://docs.microsoft.com/en-us/azure/virtual-machines/windows/managed-disks-overview

Was this article useful? Support my work!