Quickly Clone a VM in Azure !

Steps

  • Login to Azure Portal and select the VM you wish to clone.
  • Select the attached disk(s)
  • Select ‘Create snapshot’ option to create a snapshot of the disk used by the vm. OS as well any Data Disks to be selected for creating snapshots.

Now, we can create manage disk using the snapshot created in the above step. I used cloudshell to execute the required PowerShell commands as below

$resourceGroupName =’test’
$snapshotName = ‘testsnap1’
$diskName = ‘disk1’
$diskSize = ‘128’
$storageType = ‘Premium_LRS’
$location = ‘westus’
Select-AzureRmSubscription -SubscriptionId ‘fill your sub id’
$snapshot = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
$disk = New-AzureRmDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id
New-AzureRmDisk -Disk $disk -ResourceGroupName $resourceGroupName -DiskName $diskName

With the managed disk created above, we can start creating the VM by using a similar PowerShell script as below:

$virtualNetworkName = ‘VMTESTNET1’
$virtualMachineName = ‘VMTEST1’
$virtualMachineSize = ‘Standard D2s v3 (2 vcpus, 8 GiB memory)’
$VirtualMachine = New-AzureRmVMConfig -VMName $virtualMachineName -VMSize $virtualMachineSize
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Windows
$publicIp = New-AzureRmPublicIpAddress -Name ($VirtualMachineName.ToLower()+’_ip’) -ResourceGroupName $resourceGroupName -Location $snapshot.Location -AllocationMethod Dynamic
$vnet = Get-AzureRmVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName
$nic = New-AzureRmNetworkInterface -Name ($VirtualMachineName.ToLower()+’_nic’) -ResourceGroupName $resourceGroupName -Location $snapshot.Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $nic.Id
New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $resourceGroupName -Location $snapshot.Location

Tags:

No responses yet

Leave a Reply

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

Recent Comments