Migrate/Copy Azure Managed Disk between regions using PowerShell
Migrate/Copy Azure Managed Disk between regions using PowerShell GitHub - mdiloreto/copy-azdisk-from-regionContribute to mdiloreto/copy-azdisk-from-region development by creating an account on GitHub.github.comFor a personal lab, I had to m

Contribute to mdiloreto/copy-azdisk-from-region development by creating an account on GitHub.github.com
For a personal lab, I had to move an Azure Disk from the East US region to the Brazil South region. To re-region a disk in Azure, there is no automated process in the Azure backend. To do this, we must use the following documentation:
Upload a VHD to Azure or copy a disk across regions - Azure PowerShell - Azure Virtual MachinesLearn how to upload a VHD to an Azure managed disk and copy a managed disk across regions, using Azure PowerShell, via…learn.microsoft.com
In this documentation we are provided with a simplified script that makes the copy of the disk through AzCopy:
$sourceRG = <sourceResourceGroupHere>$sourceDiskName = <sourceDiskNameHere>$targetDiskName = <targetDiskNameHere>$targetRG = <targetResourceGroupHere>$targetLocate = <yourTargetLocationHere>$targetVmGeneration = "V1" # either V1 or V2#Expected value for OS is either "Windows" or "Linux"$targetOS = <yourOSTypeHere>
$sourceDisk = Get-AzDisk -ResourceGroupName $sourceRG -DiskName $sourceDiskName
# Adding the sizeInBytes with the 512 offset, and the -Upload flag$targetDiskconfig = New-AzDiskConfig -SkuName 'Standard_LRS' -osType $targetOS -UploadSizeInBytes $($sourceDisk.DiskSizeBytes+512) -Location $targetLocate -CreateOption 'Upload' -HyperVGeneration $targetVmGeneration
$targetDisk = New-AzDisk -ResourceGroupName $targetRG -DiskName $targetDiskName -Disk $targetDiskconfig
$sourceDiskSas = Grant-AzDiskAccess -ResourceGroupName $sourceRG -DiskName $sourceDiskName -DurationInSecond 86400 -Access 'Read'
$targetDiskSas = Grant-AzDiskAccess -ResourceGroupName $targetRG -DiskName $targetDiskName -DurationInSecond 86400 -Access 'Write'
azcopy copy $sourceDiskSas.AccessSAS $targetDiskSas.AccessSAS --blob-type PageBlob
Revoke-AzDiskAccess -ResourceGroupName $sourceRG -DiskName $sourceDiskName
Revoke-AzDiskAccess -ResourceGroupName $targetRG -DiskName $targetDiskNameBased on this script I created the following repo:
GitHub - mdiloreto/copy-azdisk-from-regionContribute to mdiloreto/copy-azdisk-from-region development by creating an account on GitHub.github.com
It contains a more elaborate script based on this one that allows us to simplify the execution process by using a function that has the following characteristics:
Features:
- Azure PowerShell Module Verification: Before starting copy operations, the script verifies whether the specific Azure PowerShell modules (Az.Accounts and Az.Compute) are installed and of the required version. If they are not present or outdated, it installs them automatically.
- Azure Authentication and Context: The script connects to the Azure account using the provided credentials (tenant ID and subscription ID) and sets the context for subsequent Azure PowerShell commands.
- AzCopy Check and Install: A check is performed to determine if AzCopy, a command-line tool for moving data to and from Azure storage containers, is installed. If it is not, the script proceeds with its installation.
- Disk Copy Operations: The source disk information in the current region is obtained. The destination disk is configured in the new region with the characteristics of the source disk. If the source disk has a security profile, it is transferred to the destination disk.
- Secure Disk Access: The script generates Shared Service Signed Access (SAS) tokens for both the source disk and the destination disk, allowing for secure transfer.
- Using AzCopy for Data Transfer: Use the AzCopy tool to efficiently move disk data from the source region to the destination region.
- Cleanup: Once the transfer is complete, the script revokes SAS access to maintain security.
- Error Handling: The script is designed with try-catch blocks to handle exceptions and errors that may arise during execution, providing descriptive error messages for easy debugging.
- Destination Disk Configuration: The destination disk is configured in the new region with the characteristics of the source disk.
- Security Profile: If the source disk has a security profile, it is transferred to the destination disk.
Az.AccountsAz.ComputeTry-catchDemo of use
Pre-requisitions:
- Take a snapshot of the disk in question.
- Create a disk from the snapshot.
- Clone de Repo:
git clone https://github.com/mdiloreto/copy-azdisk-from-regioncd copy-azdisk-from-regionWe will make a demo of the use:
- Basically, we’re going to copy the “latest_tw_admin_vm-osdisk” disk found in the East US region to Brazil South.

- The first thing we need to do is set the parameters of the function that are in the final lines of code before it is called.

- We can run the script, as in this example, by simply calling the .ps1 file.

- First of all, it will ask us to sign in to set the Context of the Azure Az module.

- Set the context correctly.

- Make a copy of the Security Profile. Specifically the Security Type (this was not in the original script provided by Microsoft).

- Start copying the files through the SAS URLs of each one (to see more details of how you do it, review the script and readme.md).

- After the necessary time, the contents of the disk are correctly copied and the operation ends:

- We can verify that the disk was successfully copied to the specified region.

Mateo Di Loreto
Thanks for reading. The learning continues in the next article.


