In this post, we will outline how you can copy a custom VHD image from one Azure subscription to another Azure subscription.
For more information on how to Prepare a Windows VHD or VHDX to upload to Azure, please check the following article.
Table of Contents
Introduction
The scenario is the following:
You have two Azure subscriptions, and in one of them, you already have Windows Server custom VHD images that you use to create new virtual machines. The goal is, you want to copy this image to the other Azure subscription so you can use it there to create new VMs in that subscription.
What you could do is, copy the VHD image to another storage account of the second subscription. Once the VHD is copied to the second subscription you then can create a new image using that VHD and use it to create VMs. However, this process is lengthy and you will pay extra storage costs to have the same image in two different subscriptions.
Well, there is a better approach, you can move resources from one Azure subscription to another, by selecting “Change” next to the Subscription, and then move the resources as shown in the following screenshot. If you want to create new VMs in the first subscription, then you can move it back and forth.
The move is very fast between subscriptions, it will take a couple of minutes to move 50GB fixed VHD, and then you are ready to create new VMs in the second subscription.
Move VHD From One Azure Subscription to Another
To take this step even further, we have created the following PowerShell script that will automate the entire process and create a new customized VM in Azure.
Enjoy :)
#! Variables for reuse
$location = 'West Europe'
$sourceResourceGroup = 'source-rg-01'
$destinationResourceGroup = 'destination-rg-01'
$network = "vnet-core-rg-01"
$storagediag = "weuprodlogsto01"
#! Get Blob custom VHD image
$azStorage = Get-AzStorageAccount -ResourceGroupName $sourceResourceGroup
$storageContainer = Get-AzStorageContainer -Context $azStorage.Context
$azblob = Get-AzStorageBlob -Container $storageContainer.Name -Context $azStorage.Context
#! Create the new OS disk from the uploaded VHD
$sourceUri = "$($azblob.ICloudBlob.Container.Uri.AbsoluteUri)/$($azblob.name)"
$osDiskName = 'OSDisk'
$osDisk = New-AzDisk -DiskName $osDiskName -Disk `
(New-AzDiskConfig -AccountType Premium_LRS `
-Location $location -CreateOption Import `
-SourceUri $sourceUri) `
-ResourceGroupName $destinationResourceGroup
#! Create VM resources
#! Create vNIC
$nicName = "vNic1"
$vnet = Get-AzVirtualNetwork -ResourceGroupName $network
$nsg = Get-AzNetworkSecurityGroup -ResourceGroupName $destinationResourceGroup
$nic = New-AzNetworkInterface -Name $nicName `
-ResourceGroupName $destinationResourceGroup `
-Location $location -SubnetId $vnet.Subnets[0].Id `
-NetworkSecurityGroupId $nsg.Id
#! Set the VM name and size
$vmName = "NewAzureVM"
$vmConfig = New-AzVMConfig -VMName $vmName -VMSize "Standard_D4s_v3"
#! Add the vNIC
$vm = Add-AzVMNetworkInterface -VM $vmConfig -Id $nic.Id
$vm = Set-AzVMBootDiagnostics -VM $vm -Enable -StorageAccountName $storagediag -ResourceGroupName $destinationResourceGroup
#! Add the uploaded OS disk
$vm = Set-AzVMOSDisk -VM $vm -ManagedDiskId $osDisk.Id -StorageAccountType Premium_LRS `
-DiskSizeInGB 50 -CreateOption Attach -Windows
#! Add a managed Data disk
$dataDiskName = 'DataDisk1'
$diskConfig = New-AzDiskConfig -SkuName Premium_LRS -Location $location -CreateOption Empty -DiskSizeGB 100
$dataDisk1 = New-AzDisk -DiskName $dataDiskName -Disk $diskConfig -ResourceGroupName $destinationResourceGroup
$vm = Add-AzVMDataDisk -VM $vm -Name $dataDiskName -CreateOption Attach -ManagedDiskId $dataDisk1.Id -Lun 0
#! Create the New VM
New-AzVM -ResourceGroupName $destinationResourceGroup -Location $location -VM $vm
Hope this helps!
__
Thank you for reading my blog.
If you have any questions or feedback, please leave a comment.
-Charbel Nemnom-