You dont have javascript enabled! Please enable it!

How To Move VHD Image From One Azure Subscription to Another

2 Min. Read

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.

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.

How To Move VHD Image From One Azure Subscription to Another 1

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.

How To Move VHD Image From One Azure Subscription to Another 2

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-

Photo of author
About the Author
Charbel Nemnom
Charbel Nemnom is a Senior Cloud Architect, Swiss Certified ICT Security Expert, Certified Cloud Security Professional (CCSP), Certified Information Security Manager (CISM), Microsoft Most Valuable Professional (MVP), and Microsoft Certified Trainer (MCT). He has over 20 years of broad IT experience serving on and guiding technical teams to optimize the performance of mission-critical enterprise systems with extensive practical knowledge of complex systems build, network design, business continuity, and cloud security.
Previous

Azure Security Center: How to Protect Your Datacenter with Next Generation Security

How To Assess and Migrate Hyper-V Virtual Machines Using Azure Migrate Service

Next

Let me know what you think, or ask a question...

error: Alert: The content of this website is copyrighted from being plagiarized! You can copy from the 'Code Blocks' in 'Black' by selecting the Code. Thank You!