Snapshot Managed Disk to Storage Account

This is a blogpost about creating an image from an existing Virtual Machine on Microsoft Azure. We are going to snapshot the managed disk of the machine and upload it to a Storage Account.

For example, you can use this blog/script to create an image for Citrix Cloud.

(The complete script is available at the bottom of the page)

Prerequisites

You have a VM and this VM runs on managed disks.

Create a storage account which has a container called, for example, images.

In addition, this is mine:

Storage Account to store managed disks snapshot

Example

In this section of the blog we are going to capture the image and removed the VM’s resources. This is the VM we are capturing:

Snapshot Managed Disk to Storage Account

Firstly, we need to install the modules needed to use the CMDlets. Use this code to do so:

Next we log on to Azure. I use a Service Principal for that because I want to us this in my Azure DevOps Pipeline.

After that, we need to select the proper subscription. Use this command:

Check if the VM is running and shut it down if it is.

This is the output shown:

Output Shutdown management disk

Now it is time to create the snapshot. Use this code:

This the output of the command:

Output Snapshot managed disk

Furthermore, you can see the snapshot created in the resourcegroup:

To upload this snapshot you need to following code:

This command provides the following output:

Upload output for managed disk snapshot upload

NOTE: The upload is not done after 20 seconds. To check the progress you can use this code:

This “do while” loop checks the upload status every 60 seconds and reports back whether the upload has finished. For example:

Snapshot management disk upload example

It takes about 15 – 20 minutes for the upload to complete. This is what it looks like in the storage account:

uploaded snapshot of managed disk



When the upload completes it is time to clean up the remaining resources. We can do by using the following code:

Note: This will delete all resources in the resource group. If you have mixed resources in a resourcegroup DO NOT RUN this.

This is the output:

It first states that the upload has completed and that the resource group will be cleaned. After that, the VM’s will be deleted. The snapshot access link will be revoked and the all remaining resources will be deleted.

That is how you snapshot a managed disk and upload it to a Storage Account, fully automated. 🙂

Complete Script

References

Arlan Blogs Unmanaged Disk to Storage Account

Other Blogs:
RDP ShortPath for WVD
Bitlocker Encryption Azure DevOps Release Pipeline

Related Posts

2 thoughts on “Snapshot Managed Disk to Storage Account

Leave a Reply