Windows Azure : Copy VHD between subscriptions

Last week I was playing around with LifeCycle Services and tested out the ‘Cloud hosted environments’ feature. This is an awesome feature that will save many of you some hours in prepping demo virtual machines. (For now, it is only able to deploy a template environment suited for demo purposes, but more templates will be available later on)

But, it didn’t take long until I discovered my first ‘missing feature’ here… namely I was stupid enough to link the wrong subscription to the LCS project and I ended up adding my pay-as-you-go subscription instead of the one with acual credit on it ­čśÉ Soon after that I realized there is no option to unlink it again. So there you have it, the LCS project and one of my subscriptions are married to one another now!

So I had two options here:

  • Delete the VM in Azure and redeploy a new one from LCS
  • Copy the VM from one subscription to another.

The first one was the easy one, so to have a bit of a challenge I went and chose the second option. Let’s write some PowerShell stuff to get things copied from one subscription to another! Here is how you have to do just that:

Install the Azure Powershell module

You can download and install the Azure PowerShell module by running the┬áMicrosoft Web Platform Installer. When finished you can fire it up in Windows 8 by just typing “Power” at the home screen and you will see it right away :).

Get you machine familiar with your Azure subscriptions

To be able to run the powershell commands in the next part, you will first need to import your subscription settings on your machine. Your first command here will be:

Get-AzurePublishSettingsFile

When running this cmdlet, your browser will pop up and take you to the correct link to download the publish settings files. When you have downloaded the files, run the following cmdlet to import it on your machine:

Import-AzurePublishSettingsFile "Path\subscriptionname.publishsettings"

Copy the VHD disks between subscriptions

Now that we are all set up, it’s time to start copying the virtual disks between the source and target subscription. Use the following script to do so.

### This assumes you have already imported the azure publishsettings file and the subscriptions are known on your machine.
Select-AzureSubscription "MyCoolSubscriptionName" 

### Defines the location of the VHD we want to copy
$sourceVhdUri = "https://mysourcestorageaccountname.blob.core.windows.net/omitted_part_of_the_uri/AX2012R3_DEMO_OS.vhd"

### Defines the source Storage Account
$sourceStorageAccountName = "mysourcestorageaccountname"
$sourceStorageKey = "mywaytolongsourcestoragekey"

### Defines the target Storage Account
$destStorageAccountName = "mytargetstorageaccountname"
$destStorageKey = "myalsowaytolongtargetstoragekey"

### Create a context for the source storage account
$sourceContext = New-AzureStorageContext  ÔÇôStorageAccountName $sourceStorageAccountName `
                                        -StorageAccountKey $sourceStorageKey  

### Create a context for the target storage account
$destContext = New-AzureStorageContext  ÔÇôStorageAccountName $destStorageAccountName `
                                        -StorageAccountKey $destStorageKey  

### Name for the destination storage container
$containerName = "dynamicsDeployments"

### Create a new container in the destination storage
New-AzureStorageContainer -Name $containerName -Context $destContext 

### Start the async copy operation
$blob1 = Start-AzureStorageBlobCopy -srcUri $srcUri `
                                    -SrcContext $srcContext `
                                    -DestContainer $containerName `
                                    -DestBlob "AX2012R3_DEMO_OS.vhd" `
                                    -DestContext $destContext

### Get the status of the copy operation
$status = $blob1 | Get-AzureStorageBlobCopyState 

### Output the status every 10 seconds until it is finished                                    
While($status.Status -eq "Pending"){
  $status = $blob1 | Get-AzureStorageBlobCopyState 
  Start-Sleep 10
  $status
}

If you filled in the specifics of your own subscriptions, storage accounts, … you should see the following output while copying.

CopyAzureVHDCopy the virtual machine between subscriptions

Now that have transferred the virtual disks between subscriptions, we still have to copy the virtual machine between the two. And to do that, there is a nice script available on TechNet Script Center : Copy a Virtual Machine between subcriptions

So there you have it, your VM is now copied to a second subscription and if you like, you can just remove it from the source subscription.

 

1 thought on “Windows Azure : Copy VHD between subscriptions

Leave a Reply

Your email address will not be published. Required fields are marked *