Skip to content

Instantly share code, notes, and snippets.

@astaykov
Created May 2, 2017 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astaykov/28024a5052b3e00298c182fd551d4813 to your computer and use it in GitHub Desktop.
Save astaykov/28024a5052b3e00298c182fd551d4813 to your computer and use it in GitHub Desktop.
Copy VHDs for VMs in Microsoft Cloud Deutschalnd to any other Azure location
param(
[String] $destinationUri = "https://[YOUR STORAGE ACCOUNT NAME].blob.core.windows.net/vhds",
[String] $destinationKey = "[YOUR STORAGE ACCOUNT KEY]",
[String] $sourceAccountPattern = "*disk*",
[String] $pathToAzCopy = "C:\Program Files (x86)\Microsoft SDKs\Azure\AzCopy\AzCopy.exe"
)
clear
Login-AzureRmAccount -EnvironmentName AzureGermanCloud
$storageAccounts = Get-AzureRmStorageAccount
foreach($acct in $storageAccounts)
{
Write-Debug "$($acct.StorageAccountName) | $($acct.ResourceGroupName) | $($acct.Location)"
if($acct.StorageAccountName -like $sourceAccountPattern)
{
$aKey = Get-AzureRmStorageAccountKey -ResourceGroupName $acct.ResourceGroupName -Name $acct.StorageAccountName
$primaryKey = $aKey[0].Value
$connString = "DefaultEndpointsProtocol=https;AccountName=$($acct.StorageAccountName);AccountKey=$($primaryKey);EndpointSuffix=core.cloudapi.de";
#$connString = "DefaultEndpointsProtocol=https;AccountName=$($acct.StorageAccountName);AccountKey=$($primaryKey)";
$ctx = New-AzureStorageContext -ConnectionString $connString
$containers = Get-AzureStorageContainer -Context $ctx
foreach($container in $containers)
{
Write-Debug $container.Name
Write-Debug "==========================================="
$blobs = Get-AzureStorageBlob -Container $container.Name -Context $ctx
foreach($blob in $blobs)
{
if($blob.BlobType -eq "PageBlob" -and $blob.Name -like '*.vhd')
{
Write-Debug "$($blob.BlobType) | $($blob.Name) ($($blob.Length))"
Write-Debug $blob.ICloudBlob.Container.Uri.AbsoluteUri
$cmd = """$($pathToAzCopy)"" /Source:$($blob.ICloudBlob.Container.Uri.AbsoluteUri) /Dest:$($destinationUri) /SourceKey:$($primaryKey) /DestKey:$($destinationKey) /Pattern:$($blob.Name)"
Write-Output $cmd
$src = $blob.ICloudBlob.Container.Uri.AbsoluteUri
$file = $blob.Name
& "$pathToAzCopy" /Y /V:"c:\tmp\azcopy.log" /Source:""$src"" /Dest:""$destinationUri"" /SourceKey:""$primaryKey"" /DestKey:""$destinationKey"" /Pattern:""$file""
#& $cmd
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment