How to Quickly Clone a VM in Azure !

Abhimanyu Garg

Abhimanyu

Cloud and DevOps Professional and previously a C# ASP.NET Developer with keen interest in system design and architecture. Currently involved in projects using AWS, Azure, Automation, Monitoring, REST APIs and DevOps. Email: [email protected] / [email protected]

13 Responses

  1. Srinivas M says:

    Thanks for sharing the steps. This is such a basic requirement and should be available directly on portal. Azure should pick up fast.

  2. IvanD says:

    I don’t think this is going to work for the OS disk. Maybe they’ve changed something and now it doesn’t require you to delete the VM? It would be very nice if they did.

  3. rdai111 says:

    amazing, thank you! This works! The only thing worth pointing out is in the article you broke the script into two parts but there are variables in the first script that is used in the second.

    I ran the first script, then some how my powershell timed out or something so powershell “forgot” the variables in the first

    So I had to glue the two pieces together, comment out

    $disk = New-AzureRmDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id

    New-AzureRmDisk -Disk $disk -ResourceGroupName $resourceGroupName -DiskName $diskName

    and

    #(Optional Step) Add data disk to the configuration. Enter DataDisk Id
    $VirtualMachine = Add-AzureRmVMDataDisk -VM $VirtualMachine -Name $dataDiskName -ManagedDiskId -Lun “0” -CreateOption “Attach”

    since I don’t have an optional data disk, after that I ran the whole script again and it worked.

  4. Agreed. Would have been better if I had combined the two sections of the scripts but wanted to breakdown the scripts so that they are easy to understand. Glad that it worked well for you.
    Thanks!

  5. Michael says:

    This has been a tremendous help and over a year latter, it’s still the only resource I could find to help me achieve this style of cloning.

  6. Dave Williams says:

    The instructions here will work but just be aware of one important thing:

    If you bring a new machine online that was cloned from an old machine’s OS disk, they both now have the same hostname. The new machine will remove the old machine from the directory running in Azure. This means that if you have any machines that connect to each other (e.g., a web server talking to a DB server) via its hostname, it is now broken. I know this as I have done it. This is why the generalizing step is needed.

    D.

  7. Thanks for the comment Dave. Yes this is true but there is a workaround for it.
    You can first create a clone VM with the steps mentioned in this article and then you can generalize the cloned VM and create a new VM from the image. That way you get a generalized clone VM. 🙂

  8. joshan says:

    Confused by the work around. You are still creating the cloned VM before you can generalize it. How does that stop the AD changes?

  9. Jaime says:

    hi, when running this setp: #Create a new Managed Disk from the Snapshot provided
    $disk = New-AzureRmDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id

    I get the following error:
    Cannot convert ‘System.Object[]’ to the type ‘System.String’ required by parameter ‘SourceResourceId’. Specified method is not supported.

    What has changed?

    Thanks in advance

  10. Hi @Jaime, this is a general powershell related error and not azure specific. Can you please recheck the commands you have executed and make sure there are no invalid characters.
    These commands should work fine.

    Thanks

  11. Lada Korine says:

    Have a question. By cloning the exiting machine, we are ending up with 2 machines now. Can those 2 machines run in the same time?

  12. Yes they can because they will have different internal IP. Do you see any challenge with that?

Leave a Reply