Friday, 2 June 2017

Copying Azure Images across subscriptions

There have been multiple blogs on this on internet but still I faced problems during assigning values to variable or while checking order of commands. So here comes my blog with sample values and easy to understand and use.
Here we will see , how you can copy Images(internally vhd) from one subscription to another.
Steps to follow:-
1.  Define Source Subscription Details and create Source Context
$sourceSubscriptionName = "MySourceSubscriptionName"
$sourceStorageAccountName = "mydemostorage"
$sourceStorageAccountKey = "000000000001111111111111112222222222233333333333334444444444"
$sourceContainerName = "mycontainer"
$sourceBlobName = "MyImage.vhd"
$sourceBlobUrl = "https://mydemostorage.blob.core.windows.net/mycontainer/MyImage.vhd"
 
# Set the subscription context to the source subscription
Select-AzureSubscription -SubscriptionName $sourceSubscriptionName
$sourceStorageContext = New-AzureStorageContext -StorageAccountName $sourceStorageAccountName -StorageAccountKey $sourceStorageAccountKey -Protocol Https
Write-Host "Source Context created." -ForegroundColor Green
You can get the storage account key from Azure management portal. Go to Storage tab->Select your Storage-> Manage Access Keys –> Copy Primary Access key.
To get information about Source Image. Go to Virtual Machine Tab-> Images-> Select Image –> On details page you can see the vhd location which is the blob url. This url also helps you to identify blob name, storage account name and container name.
2. Define Destination Subscription Details and create destination Context
$destinationSubscriptionName = "MyDestinationSubscriptionName"
$destinationStorageAccountName = "mydeststorage"
$destinationStorageAccountKey = "555555555555666666666666777777777777888888888999999"
$destinationContainerName = "mydestcontainer"
$destinationBlobName = "MyDestImage.vhd"
 
# Set the subscription context to the destination subscription
Select-AzureSubscription -SubscriptionName $destinationSubscriptionName
$destinationStorageContext = New-AzureStorageContext -StorageAccountName $destinationStorageAccountName -StorageAccountKey $destinationStorageAccountKey -Protocol Https
Write-Host "Destination Context created." -ForegroundColor Green
3. Create Destination container if not exists
# Ensure the destinationContainer is available
$existingContainer = Get-AzureStorageContainer -Name $destinationContainerName -Context $destinationStorageContext
if($existingContainer -eq $null)
{
    Write-Host "Creating Container..." -NoNewline
    $destContainer = New-AzureStorageContainer -Name $destinationContainerName -Context $destinationStorageContext -ErrorAction SilentlyContinue | Out-Null
    Write-Host "done" -ForegroundColor Green
}
else
{
    Write-Host "Container already exists..." -ForegroundColor Green
}
4. Start Copying the Blob vhd
try
{
    Write-Host "Starting Copying vhd..." -NoNewline
    $copyOperation = Start-AzureStorageBlobCopy `
                        -SrcContext $sourceStorageContext `
                        -SrcBlob $sourceBlobName `
                        -SrcContainer $sourceContainerName `
                        -DestContext $destinationStorageContext `
                        -DestContainer $destinationContainerName `
                        -DestBlob $destinationBlobName `
                        -FORCE
 
    Write-Host "Your vhd has been Copied Successfully. Please create Image through Portal and use it." -ForegroundColor Green
}
catch
{
    Write-Host "Copying Failed." -ForegroundColor Red
}
After this command is executed successfully, you may need to wait for some time for copy operation to be completed. Though your PowerShell window will complete execution within no time, the copy operation in Azure takes some time to complete. The whole script will look like below:-
$sourceSubscriptionName = "MySourceSubscriptionName"
$sourceStorageAccountName = "mydemostorage"
$sourceStorageAccountKey = "000000000001111111111111112222222222233333333333334444444444"
$sourceContainerName = "mycontainer"
$sourceBlobName = "MyImage.vhd"
$sourceBlobUrl = "https://mydemostorage.blob.core.windows.net/mycontainer/MyImage.vhd"
 
# Set the subscription context to the source subscription
Select-AzureSubscription -SubscriptionName $sourceSubscriptionName
$sourceStorageContext = New-AzureStorageContext -StorageAccountName $sourceStorageAccountName -StorageAccountKey $sourceStorageAccountKey -Protocol Https
Write-Host "Source Context created." -ForegroundColor Green
 
$destinationSubscriptionName = "MyDestinationSubscriptionName"
$destinationStorageAccountName = "mydeststorage"
$destinationStorageAccountKey = "555555555555666666666666777777777777888888888999999"
$destinationContainerName = "mydestcontainer"
$destinationBlobName = "MyDestImage.vhd"
 
# Set the subscription context to the destination subscription
Select-AzureSubscription -SubscriptionName $destinationSubscriptionName
$destinationStorageContext = New-AzureStorageContext -StorageAccountName $destinationStorageAccountName -StorageAccountKey $destinationStorageAccountKey -Protocol Https
Write-Host "Destination Context created." -ForegroundColor Green
 
# Ensure the destinationContainer is available
$existingContainer = Get-AzureStorageContainer -Name $destinationContainerName -Context $destinationStorageContext
if($existingContainer -eq $null)
{
    Write-Host "Creating Container..." -NoNewline
    $destContainer = New-AzureStorageContainer -Name $destinationContainerName -Context $destinationStorageContext -ErrorAction SilentlyContinue | Out-Null
    Write-Host "done" -ForegroundColor Green
}
else
{
    Write-Host "Container already exists..." -ForegroundColor Green
}
 
try
{
    Write-Host "Starting Copying vhd..." -NoNewline
    $copyOperation = Start-AzureStorageBlobCopy `
                        -SrcContext $sourceStorageContext `
                        -SrcBlob $sourceBlobName `
                        -SrcContainer $sourceContainerName `
                        -DestContext $destinationStorageContext `
                        -DestContainer $destinationContainerName `
                        -DestBlob $destinationBlobName `
                        -FORCE
 
    Write-Host "Your vhd has been Copied Successfully. Please create Image through Portal and use it." -ForegroundColor Green
}
catch
{
    Write-Host "Copying Failed." -ForegroundColor Red
}
Once your vhd is copied. Follow below steps to create Image:-
1. Go To Virtual Machine Tab –> Images
2. Click on create –> Give Name and Description –> Browse VHD, just copied using above steps.
3. Click on OK

Your Image is ready, you can now use it to create Virtual Machines. Happy Coding!!!

No comments:

Post a Comment