UPDATED 11/10/2016 @11:24 PM EST (fixed mis-matched “$SrcBlob” and “$SourceBlob” references due to lack of sleep, chest cough, angry dog, bad posture and did I mention lack of sleep?  Yes, I did.  Apologies for any angst that caused).

Sometimes you just need to copy a blah blah blah blob from one Resource Group / Storage Account / Container to another.  For example, while having to spin up VM’s using source VHD’s whereby some VMs are in different storage accounts, and none are in the same storage account as where the original VHD resides.  Mouthful? Yes.

  Copy-AzureBlob copies a Blob file from one resource group, storage account
  and container, to another resource group, storage account, and container.

  [string] Name of blob (file) to copy from source container.

.PARAMETER SrcResourceGroupName
  [string] Name of Resource Group where Source storage account resides.

.PARAMETER SrcContainerName
  [string] Name of Container under Source storage account where 
  Source file resides.

.PARAMETER SrcStorageAccountName
  [string] Name of Storage Account which contains the Source File to copy.

.PARAMETER DestResourceGroupName
  [string] Name of Resource Group where Destination Storage Account 
  and Container reside, in which Source File will be copied to.

.PARAMETER DestContainerName
  [string] Name of Destination Container where Source File will be copied to.
.PARAMETER DestStorageAccountName
  [string] Name of Destination Storage Account which holds the Storage Container
  where Source File will be copied to.

  [switch] Forces overwrite of existing Source File in the destination container.

  Requires a valid Azure RM login session.
  If Source File exists in the specified destination (container) 
  it will not be overwritten

param (
  [parameter(Mandatory=$True)] [string] $SourceBlob,
  [parameter(Mandatory=$True)] [string] $SrcResourceGroupName,
  [parameter(Mandatory=$True)] [string] $SrcContainerName,
  [parameter(Mandatory=$True)] [string] $SrcStorageAccountName,
  [parameter(Mandatory=$True)] [string] $DestResourceGroupName,
  [parameter(Mandatory=$True)] [string] $DestContainerName,
  [parameter(Mandatory=$True)] [string] $DestStorageAccountName,
  [parameter(Mandatory=$True)] [switch] $OverWrite
$SourceStorageKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $SrcResourceGroupName -Name $SrcStorageAccountName)[0].Value
$DestStorageKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $DestResourceGroupName -Name $DestStorageAccountName)[0].Value
$SourceStorageContext = New-AzureStorageContext –StorageAccountName $SourceStorageAccount -StorageAccountKey $SourceStorageKey
$DestStorageContext = New-AzureStorageContext –StorageAccountName $DestStorageAccountName -StorageAccountKey $DestStorageKey
$Blobs = (Get-AzureStorageBlob -Context $SourceStorageContext -Container $SrcContainerName | ?{$_.Name -eq $SourceBlob})
$BlobCpyAry = @()
$DestBlobs = (Get-AzureStorageBlob -Context $DestStorageContext -Container $DestContainerName | ?{$_.Name -eq $SourceBlob})
if ((!($OverWrite)) -and ($DestBlobs -ne $null)) {
   Write-Output "$SourceBlob already exists in destination."
else {
  foreach ($Blob in $Blobs) {
    Write-Output "info: Copying $Blob.Name"
    $BlobCopy = Start-CopyAzureStorageBlob -Context $SourceStorageContext `
      -SrcContainer $SourceContainer -SrcBlob $Blob.Name `
      -DestContext $DestStorageContext -DestContainer $DestContainer `
      -DestBlob $Blob.Name -Force
      $BlobCpyAry += $BlobCopy
  foreach ($BlobCopy in $BlobCpyAry) {
    $CopyState = $BlobCopy | Get-AzureStorageBlobCopyState
    $Message = $CopyState.Source.AbsolutePath + " " + $CopyState.Status + `
      " {0:N2}%" -f (($CopyState.BytesCopied/$CopyState.TotalBytes)*100) 
    Write-Output $Message

Leave a Reply

Please log in using one of these methods to post your comment:

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