PowerShell script for creating a new Azure VM using a disk snapshot

Creating a new snapshot using an existing disk of an Azure virtual machine (VM) requires three steps. First we have to create a snapshot of a VM disk, next we create a managed disk using this snapshot, and then we can create the VM using this managed disk. At the end of the post you will find a PowerShell script that lets you automate the task.
Latest posts by Baki Onur Okutucu (see all)

Azure supports both managed and unmanaged disks when it comes to creating a new VM. You create unmanaged disks in storage accounts, so you have to manage the storage account in which the disk resides as well. With managed disks, you don't need to worry about managing storage accounts. Instead, you just manage the disk itself, which represents an independent virtual hard drive.

Using managed disks with VMs has a number of advantages such as capabilities of automatic storage service encryption, role-based account control features, availability set support, and so on.

You can select managed disks during a new VM deployment.

Managed disk as an option during VM deployments

Managed disk as an option during VM deployments

After creating a VM with a managed disk, you can then find disks and manage them separately in the "Disks" section in Azure.

Manage disks in the Disks section in Azure

Manage disks in the Disks section in Azure

We will use one of the disks shown above to create a new snapshot. A VM named Windows10 uses this disk and displays it as "Owner VM."

Managed Disk details

Managed Disk details

Creating a snapshot using a VM Disk ^

So what we need to do first is specify the VM details with which we will create a new snapshot using the existing managed disk. This operation basically creates a new copy of an existing VM disk, which could be either an OSDisk or a DataDisk.

After creating a new snapshot named "OnurSnapshot1" using the script block above, it should appear in the Azure "Snapshots" with the details such as source disk, creation time, and disk size.

Disk snapshots

Disk snapshots

Creating a managed disk using a snapshot ^

As the second step of our scenario, we will now create a new disk using this snapshot we've just created. This way, we should be able to attach that pre-created disk to a new VM during deployment. The following script block will create a new disk using the snapshot.

Now we have a new disk ready to attach to a new VM we will deploy.

A new disk created from a snapshot

A new disk created from a snapshot

Creating a VM using a managed disk ^

The last step requires us to attach the existing disk we've created to a new VM. So the script below will create a new VM using an existing VNet, subnet, and a managed disk we've created earlier.

We're now creating a new VM with an existing managed disk.

A new VM created using an existing disk

A new VM created using an existing disk

After completing the deployment, we can now see the managed disk attached to the VM.

Disk information for a new VM

Disk information for a new VM

Below is the entire script for creating a new Azure VM based on a disk snapshot:

Want to write for 4sysops? We are looking for new authors.

Read 4sysops without ads and for free by becoming a member!

6+
avatar
Share
4 Comments
  1. Piyush 2 years ago

    Bro, but I cannot ssh to the newly created VM. So how did you solve this issue?

    0

  2. Paul 2 years ago

    Works great, thank you!

    I found I had to change

    Location

    in section 3 to

    -Location

    though (just in case others run into the same problem)

    So this line:

    I changed to:

    Thanks again,
    Regards

    0

  3. lavolo 2 years ago

    Thank you so much, you're so strong !

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account