Created
January 25, 2018 01:24
-
-
Save morgansimonsen/203983b7c52d1fce497c3a9fd4c24b21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CopyManagedDisk.ps1 | |
# Morgan Simonsen | |
# morgansimonsen.com | |
# | |
# Copies an Azure managed disk from one Azure region to another via a storage account | |
[CmdletBinding()] | |
Param( | |
[Parameter(Mandatory=$True,Position=1, | |
HelpMessage="Source resource group where the managed disk(s) to copy is.")] | |
[ValidateNotNullOrEmpty()] | |
[string]$sourceResourceGroupName, | |
[Parameter(Mandatory=$True,Position=2, | |
HelpMessage="Target resource group you want to copy the managed disk(s) to.")] | |
[ValidateNotNullOrEmpty()] | |
[string]$targetResourceGroupName, | |
[Parameter(Mandatory=$True,Position=3, | |
HelpMessage="The Azure location (region) of the new managed disk(s).")] | |
[ValidateNotNullOrEmpty()] | |
[string]$targetLocation, | |
[Parameter(Mandatory=$True,Position=4, | |
HelpMessage="One or more disks to copy.")] | |
[ValidateNotNullOrEmpty()] | |
[string[]]$disks , | |
[Parameter(Mandatory=$False,Position=5, | |
HelpMessage="SKU for the target disks")] | |
[ValidateNotNullOrEmpty()] | |
[ValidateSet("StandardLRS", "PremiumLRS")] | |
[string]$targetDiskSKU="PremiumLRS" | |
) | |
# user configurable variables | |
$targetStorageAccountSKU = "Premium_LRS" | |
$targetStorageAccountVHDContainer = "vhds" | |
$managedDiskSASDuration = 3600 | |
# set up destination storage account | |
$targetStorageAccountName = ("mdcopy"+(Get-Random -Minimum 10000 -Maximum 99999)) | |
Write-Output "Creating storage account $targetStorageAccountName in target location $targetLocation..." | |
$targetStorageAccount = New-AzureRmStorageAccount -Name $targetStorageAccountName -Location $targetLocation -SkuName $targetStorageAccountSKU -ResourceGroupName $targetResourceGroupName -Tag @{ Description="Temporary account for copying managed disks." } | |
$targetStorageAccountKey = $targetStorageAccount | Get-AzureRmStorageAccountKey | |
$targetStorageAccountContext = New-AzureStorageContext –StorageAccountName $targetStorageAccountName -StorageAccountKey $targetStorageAccountKey[0].Value | |
$targetContainer = New-AzureStorageContainer -Context $targetStorageAccountContext -Name $targetStorageAccountVHDContainer | |
Write-Progress -Id 0 -Activity "Copying disks..." -PercentComplete -1 | |
# Loop through disks and copy | |
ForEach ( $disk in $disks ) | |
{ | |
$SourceManagedDisk= Get-AzureRMDisk -ResourceGroupName $sourceResourceGroupName -DiskName $disk | |
$managedDiskSAS = $SourceManagedDisk| Grant-AzureRmDiskAccess -DurationInSecond $managedDiskSASDuration -Access Read | |
$targetDiskCopy = Start-AzureStorageBlobCopy -AbsoluteUri $managedDiskSAS.AccessSAS -DestContainer $targetStorageAccountVHDContainer -DestContext $targetStorageAccountContext -DestBlob $disk | |
$diskCopyStatus = ($targetDiskCopy | Get-AzureStorageBlobCopyState) | |
$copyStart = Get-Date | |
Do | |
{ | |
$diskCopyStatus = ($targetDiskCopy | Get-AzureStorageBlobCopyState) | |
Write-Progress -Id 1 -Activity $disk -Status "Progress:" -CurrentOperation ("Bytes copied: "+$diskCopyStatus.BytesCopied+" of "+$diskCopyStatus.TotalBytes) -PercentComplete ($diskCopyStatus.BytesCopied/$diskCopyStatus.TotalBytes*100) -ParentId 0 | |
Start-Sleep -Seconds 5 | |
} | |
Until | |
( | |
($diskCopyStatus.Status -eq "Success" -or ($copyStart.AddSeconds($managedDiskSASDuration) -lt (Get-Date))) | |
) | |
Write-Progress -Id 1 -Activity $disk -Completed | |
Write-Progress -Id 2 -Activity "Creating managed disk..." -PercentComplete -1 -ParentId 1 | |
$targetDiskConfig = New-AzureRmDiskConfig -Location $targetLocation -SkuName $targetDiskSKU -OsType $SourceManagedDisk.OsType -DiskSizeGB $SourceManagedDisk.DiskSizeGB -CreateOption Import -SourceUri ($targetStorageAccountContext.BlobEndPoint+$targetStorageAccountVHDContainer+"/"+$disk) | |
$targetDisk = New-AzureRmDisk -Disk $targetDiskConfig -DiskName $disk -ResourceGroupName $targetResourceGroupName | |
Write-Progress -Id 2 -Activity "Creating managed disk..." -Completed -ParentId 1 | |
} | |
Write-Progress -Id 0 -Activity "Copying disks..." -Completed | |
Write-Output "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment