How To : Move managed OSdisk from one subscription to another in Azure

#1

This is a KB article for myself to keep notes on a solution for moving VM’s between two separate subscriptions / tenants in Microsoft Azure

I will tidy this up as i go along and add images for guidance where appropriate - feel free to ask any questions.

  1. Create temporary standard storage container in the original subscription
  2. Copy the Manage OSDisk to vhd blob in the new storage :

$diskName = “Name Of OSDisk”
$token = Grant-AzureRmDiskAccess -ResourceGroupName “Resource Group” -DiskName $diskName -Access Read -DurationInSecond 36000
$storageAccount = Get-AzureRmStorageAccount -ResourceGroupName “Resource Group” -Name “Storage Account”
$copyBlob = Start-AzureStorageBlobCopy -AbsoluteUri $token.AccessSAS -DestContainer ‘Container Name’ -DestContext $storageAccount.Context -DestBlob “$diskName.vhd”
$copyBlob | Get-AzureStorageBlobCopyState -WaitForComplete

  1. Now we have a VHD of the disc, we can obtain the URL for the vhd on the storage and use with AZCopy program to move the vhd to new storage account in destination subscription

Azcopy /source:sourceurl /Dest:desturl /sourcekey:sourcekey /destkey:destkey /pattern:patternforfilesearch

  1. Now you have the VHD in your new subscription / Tennant you need to build a VM and attach the disk.
  2. First off in Azure UI create a nic for your VM and create a security group and associate with the nic.
  3. Now you need to create the VM. attach the nic and the OSDisk :

$subID = ‘subscriptionID’
$rgname = ‘resourcegroupname’
$loc = ‘DC Location’
$vmsize = ‘VM Size Name’
$vmname = ‘VM Name’
$nic1 = ‘nic name you created’
$osDiskName = ‘os disk name’
$osDiskVhdUri = ‘OS Disk Url form new storage’
$subnet = ‘subnet string’
Get-AzureRmSubscription –SubscriptionID “$subID” | Select-AzureRmSubscription
$vm = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize;
$nic1 = Get-AzureRmNetworkInterface -Name ($nic1) -ResourceGroupName $rgname;
$nic1Id = $nic1.Id;
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic1Id;
$vm = Set-AzureRmVMOSDisk -VM $vm -VhdUri $osDiskVhdUri -name $osDiskName -CreateOption attach -Windows
New-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $vm -Verbose

Once the above is all done, you should hopefully have a working VM in your new subscription.

Tidy up :

  1. Delete storage in old subscription
  2. In Azure UI navigate to disk and set disk to managed disc
  3. Delete temp storage and VHD’s in new subscription once all disc are managed by Azure

To Do :

  1. Link reference/source for the above
  2. Add more steps with images
1 Like