0
votes

I have created 2 Virtual Machines (VMs) using RM (Resource Manager) deployment method. How can I clone a VM using Azure PowerShell scripts?

2
Although a slightly different question, the answer to this is probably a duplicate of - stackoverflow.com/questions/36057736Michael B

2 Answers

1
votes

You can follow the documentation guide here: https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-windows-capture-image/

After you Open the Azure PowerShell and login to your Azure account. You can find the subscriptions your Azure account has by using the command Get-AzureRmSubscription.

Next you will need to deallocate the resources used by this virtual machine.

Stop-AzureRmVM -ResourceGroupName YourResourceGroup -Name YourWindowsVM

Next you need to set the status of the virtual machine to Generalized. Note that you will need to do this because the generalization step above (sysprep) does not do it in a way that Azure can understand.

Set-AzureRmVm -ResourceGroupName YourResourceGroup -Name YourWindowsVM -Generalized

Next, Capture the virtual machine image to a destination storage container using this command.

Save-AzureRmVMImage -ResourceGroupName YourResourceGroup -VMName YourWindowsVM -DestinationContainerName YourImagesContainer -VHDNamePrefix YourTemplatePrefix -Path Yourlocalfilepath\Filename.json

The -Path variable is optional and can be used to save the JSON template locally. The -DestinationContainerName variable is the name of the container that you want to hold your images in. The URL of the image stored will be similar to https://YourStorageAccountName.blob.core.windows.net/system/Microsoft.Compute/Images/YourImagesContainer/YourTemplatePrefix-osDisk.xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.vhd. It will be created in the same storage account as that of the original virtual machine.

0
votes

The Azure documentation offers a detailed guide to do this, so follow the instructions and in case of trouble, you can ask me!
BEWARE! When you sysprep your machine and make it generalized, you CANNOT UnDO this!!!
This means that you VM will stay stopped (deallocated) and you can create infinite cloned from this, but you cannot start this again, so make sure it is not in production :)