How To Move VHD Image From One Azure Subscription to Another

3 min read

In this post, I 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.

The scenario is the following:

You have two azure subscriptions, and in one them, you already have Windows Server custom VHD image 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 an new image using that VHD and use it to create VMs. However, this process is lengthy and you will pay extra storage cost 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 couple of minutes to move 50GB fixed VHD, and then you are ready to create new VMs in the second subscription.

To take this step even further, I 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-

Advertisements
About Charbel Nemnom 475 Articles
Charbel Nemnom is a Cloud Architect and Microsoft Most Valuable Professional (MVP), totally fan of the latest's IT platform solutions, accomplished hands-on technical professional with over 17 years of broad IT Infrastructure experience serving on and guiding technical teams to optimize performance of mission-critical enterprise systems. Excellent communicator adept at identifying business needs and bridging the gap between functional groups and technology to foster targeted and innovative IT project development. Well respected by peers through demonstrating passion for technology and performance improvement. Extensive practical knowledge of complex systems builds, network design and virtualization.

Be the first to comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.