Copying image file between subscriptions

[String]$sourcesubID=”ProvideSourcesubscriptionID”

[String]$destinationsubID=”ProvideDestinationSubscriptionID”

#Source

$sourcestorageaccount=”teststorageaccount”

$sourcesubscriptionname=”Some Subscription”

$sourceimage =”Some sourceimage”

$destinationimage=”somedestinationimage.vhd”

#destination

$destinationAffinityGroup=”GiveDestinationAffinityGroup”

$destinationstorageaccount=”destinationstorage”

$destinationSubscriptionName=”Some DestinationAzure”

$destinationcontainername=”copiedvhd”

 

#common

$thumbprint=’Grab Thumbprint from Azure portal’

#get management certificate

$managementCertificate = Get-Item “cert:\CurrentUser\MY\$($thumbprint)”

Select-AzureSubscription -SubscriptionName $sourcesubscriptionname

Set-AzureSubscription -SubscriptionName $sourcesubscriptionname -SubscriptionId $sourcesubID -Certificate $managementCertificate

 

#create source storage context

$sourcestoragekey = (Get-AzureStorageKey -StorageAccountName $sourcestorageaccount).Primary

$sourcestoragecontext = New-AzureStorageContext –StorageAccountName $sourcestorageaccount -StorageAccountKey $sourcestoragekey -Protocol Https

 

#Get Image location and extract Media location

$img=Get-AzureVmImage -Imagename $sourceimage

$imglocation = $img.MediaLink

 

 

#Create Account in Destination Subscription for Copying

Select-AzureSubscription -SubscriptionName $destinationSubscriptionName

Set-AzureSubscription -SubscriptionName $destinationSubscriptionName -SubscriptionId $destinationsubID -Certificate $managementCertificate

 

#Create Storage account in destination subscription to Store the Image

 

 

if(-not (Test-AzureName -Storage $destinationstorageaccount))

{

$AG= (Get-AzureAffinityGroup).Name | Select-Object -Last 1

 

New-AzureStorageAccount -StorageAccountName $destinationstorageaccount -AffinityGroup $AG

Set-AzureStorageAccount -GeoReplicationEnabled $false -StorageAccountName $destinationstorageaccount

 

#create destination storage context

$destinationstoragekey = Get-AzureStorageKey -StorageAccountName $destinationstorageaccount

$destinationstoragecontext = New-AzureStorageContext –StorageAccountName $destinationstorageaccount -StorageAccountKey $destinationstoragekey.Primary

}

 

#create destination storage context

$destinationstoragekey = Get-AzureStorageKey -StorageAccountName $destinationstorageaccount

$destinationstoragecontext = New-AzureStorageContext –StorageAccountName $destinationstorageaccount -StorageAccountKey $destinationstoragekey.Primary

New-AzureStorageContainer -Name $destinationcontainername -Context $destinationstoragecontext

 

$blob1=Start-CopyAzureStorageBlob -SrcContext $sourceStorageContext -SrcBlob $img.MediaLink.AbsoluteUri -SrcContainer “communityimages” -DestContext $destinationstoragecontext -DestContainer $destinationcontainername -DestBlob $destinationimage

### 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

}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s