Copying VHDs between private storage accounts

If you ever need to copy VHDs between two different storage account, you can use the PowerShell script below to do it in few steps:

Perform the copy

Firstly select the subscription you need to work with.

Select-AzureSubscription "MySubscriptionName"  

Define the source VHD - authenticated container. You can get the vhd name and url from the portal

$srcUri = "https://mysubscription.blob.core.windows.net/vhds/yourvhdname.vhd" 

Define the source Storage Account. This is where the vhd is currently located. The Private key can be acquired from the azure portal

$srcStorageAccount = "sourceStorageAccount"
$srcStorageKey = "GET KEY FROM PORTAL"

Define the destination Storage Account. This is where the vhd will be copied to. Again, the private key can be acquired from the azure portal

$destStorageAccount = "destinationStorageAccount"
$destStorageKey = "GET KEY FROM PORTAL"

Create the source storage account context

$srcContext = New-AzureStorageContext `
    -StorageAccountName $srcStorageAccount `
    -StorageAccountKey $srcStorageKey 

Create the destination storage account context

$destContext = New-AzureStorageContext `
    -StorageAccountName $destStorageAccount `
    -StorageAccountKey $destStorageKey 

Define the destination Container Name

$containerName = "vhds"

Create the container on the destination ###

New-AzureStorageContainer -Name $containerName -Context $destContext  

Start the asynchronous copy - specify the source authentication with -SrcContext ###

$blob1 = Start-AzureStorageBlobCopy `
 -srcUri $srcUri `
 -SrcContext $srcContext `
 -DestContainer $containerName `
 -DestBlob "DestinationVhdName.vhd" `
 -DestContext $destContext

Checking the status of the copy

The script below can be used to retrieve the status of the copy operation. First we define a $status variable and then we loop until the status changes to non-pending. This variable is also used to display/print the current status on the console.

Retrieve the current status of the copy operation

$status = $blob1 | Get-AzureStorageBlobCopyState 

Print out status

$status 
While($status.Status -eq "Pending"){  
 $status = $blob1 | Get-AzureStorageBlobCopyState 
 Start-Sleep 10
 $status
}

Happy coding...


  • Share this post on
comments powered by Disqus