Introduction

Azure subscription is like a prepaid account. You required balance in it before utilizing its resourses. Sometime its account balance (Azure credits) is going to expire, and once it goes to zero you will not be able to use its resources, until you will refill account again.

If you have more than one azure subscription and you don’t want to refill your account, you can move your resource from one account to another account. Here we are migrating Azure VM from one Azure subscription to another subscription. Please note, if any of your subscription is disabled, due to zero credits, this migration will not work.

Steps to Migrate a VM on another subscription

This process contains the following three steps:

Migrating VM’s blob on another subscription

To migrate blob which is used by Virtual machine, follow the steps in PowerShell.

Connect to Subscription by PowerShell

Get the subscriptions details

Get the current Subscription

For more details refer link.

Get the Current subscription id

(Get-AzureSubscription -Current).SubscriptionId

Set current Subscription

If you have more than one subscription, and your current subscription is different from your required subscription, then set your desired subscription as the current subscription:

List all virtual machines in current Subscription

Get-AzureVM

It will display all VMs with service name and VM name. Please check VM which you want to migrate and exist here.

AzureVM

To Know a VM’s Storage Account Name

To migrating a VM we require VM storage account name, execute the following command to get it.

  1. $disk = Get-AzureDisk | Where-Object { $_.AttachedTo.RoleName -eq "VMName" }
  2. $mediaLink = $disk.MediaLink
  3. $storageAccountName = $mediaLink.Host.Split('.')[0]
  4. $storageAccountName
storageAccountName

Get container and vhd file details of your VM

To migrate a VM we also container name and vhd file name. Get it by the following command:

Get-AzureVM –ServiceName “anmolwin8vs” –Name “anmolwin8vs” | Get-AzureOSDisk

command prompt

MediaLink value is the complete path of vhd file; you can see vhd file name here with .vhd extension and container name just before .vhd file. Here container name is vhds, and VHD file name is anmolwin8vs-anmolwin8vs-2015-09-01.vhd.

VHD blob copy from one Subscription to other
  1. # Source VHD blob#
  2. $blobName = "vhd file name"
  3. # Provide Source Storage Account details #
  4. $sourceStorageAccountName = "source Account name"
  5. $sourceAccountKey = "Source Storage Account Key"
  6. $sourceAccountContext = New-AzureStorageContext –StorageAccountName $sourceStorageAccountName -StorageAccountKey $sourceAccountKey
  7. $sourceContainer = "Container Name"
  8. # Provide Destination Storage Account Details #
  9. $destinationStorageAccountName = "Destniation Account Name"
  10. $destinationAccountKey = "Destination Storage Account Key"
  11. $destinationContext = New-AzureStorageContext –StorageAccountName $destinationStorageAccountName -StorageAccountKey $destinationAccountKey
  12. # Copy the blob #
  13. $blobCopy = Start-AzureStorageBlobCopy -DestContainer $destinationContainerName `
  14. -DestContext $destinationContext `
  15. -SrcBlob $blobName `
  16. -Context $sourceContext `
Example
  1. # VHD blob to copy #
  2. $blobName = "anmolwin8vs-anmolwin8vs-2015-09-01.vhd"
  3. # Source Storage Account Information #
  4. $sourceStorageAccountName = "anmolstorageacc"
  5. $sourceKey = "rF4Xp4sfozYorNQaq1OJ7PfN/IXhY9HPUhP3H7njU5ogD9Q1VBp2yhn9v/FSeHt5bKklS2tvKe1qMv3aNXQ5Xg=="
  6. $sourceContext = New-AzureStorageContext –StorageAccountName $sourceStorageAccountName -StorageAccountKey $sourceKey
  7. $sourceContainer = "vhds"
  8. # Destination Storage Account Information #
  9. $destinationStorageAccountName = "portalvhds8mzfbcnfbn3f1"
  10. $destinationKey = "dPFG2UqP/EseJyGs5Bpx3URYzycyvdHoRKKFG7tHPW6oudWGRKfHZnqUHIXphOKkiL8GqP841Dwddm8rv9xDoA=="
  11. $destinationContext = New-AzureStorageContext –StorageAccountName $destinationStorageAccountName -StorageAccountKey $destinationKey
  12. # Create the destination container #
  13. $destinationContainerName = "vhds"
  14. # Copy the blob #
  15. $blobCopy = Start-AzureStorageBlobCopy -DestContainer $destinationContainerName `
  16. -DestContext $destinationContext `
  17. -SrcBlob $blobName `
  18. -Context $sourceContext `
  19. -SrcContainer $sourceContainer
In case, destination Container Name does not exist, run the following command to create a new one:
  1. $destinationContainerName = "vhds"
  2. New-AzureStorageContainer -Name $destinationContainerName -Context $destinationContext -Permission Blob
Blob copy using AzCopy tool

You can also copy blob by using AzCopy tool.

Example:

AzCopy /Source:https://anmolstorages.blob.core.windows.net/ anmolstorageacc /Dest:https://anmolmachineVMonkiritmac.blob.core.windows.net/destinationvhds8 /SourceKey:s2NgY75yIY1uPisP79mxljQ7yK6VqCLbVfY4a247AJtqa4ug+Dvg1GYvondmW4Dn0gxpJEBYm81bAuY9GVIqIw== /DestKey:q5EQPhhJPQVKwgqgSEpDvLWUoGKAaHR3YR1BP5+aRqi/Mzjp19PKbOQWfhBb4VEPrKf7DEMudaypChvkdDQcqw== /Pattern:AnmolMachineVS-AnmolMachineVS-0-20150713.vhd

code

Create a new disk on target subscription for the blob

Till now, we have just moved a blob to new subscription; we have to create an Azure disk to boot from it. For this on azure portal here are the steps:

You can also use the following PowerShell command for creating disk:

Add-AzureDisk -DiskName "anmolWin8Disk" `
-OS Windows `
-MediaLocation "https://portalvhds8mzfbcnfbn3f1.blob.core.windows.net/vhds/ Fileanmolwin8vs-anmolwin8vs-2015-09-01.vhd " `
-Verbose


Verbose

Create the Virtual Machine with the created disk

To create VM with migrated disk, here are the steps in Azure management portal: