Copy managed disks to same or different subscription with CLI
This script copies a managed disk to same or different subscription but in the same region. The copy works only when the subscriptions are part of same AAD tenant.
To run this sample, make sure you have installed the latest version of the Azure CLI. To start, run
az login to create a connection with Azure.
This sample works in a Bash shell. For options on running Azure CLI scripts on Windows client, see Install the Azure CLI on Windows.
#Provide the subscription Id of the subscription where managed disk exists sourceSubscriptionId=dd80b94e-0463-4a65-8d04-c94f403879dc #Provide the name of your resource group where managed disk exists sourceResourceGroupName=mySourceResourceGroupName #Provide the name of the managed disk managedDiskName=myDiskName #Set the context to the subscription Id where managed disk exists az account set --subscription $sourceSubscriptionId #Get the managed disk Id managedDiskId=$(az disk show --name $managedDiskName --resource-group $sourceResourceGroupName --query [id] -o tsv) #If managedDiskId is blank then it means that managed disk does not exist. echo 'source managed disk Id is: ' $managedDiskId #Provide the subscription Id of the subscription where managed disk will be copied to targetSubscriptionId=6492b1f7-f219-446b-b509-314e17e1efb0 #Name of the resource group where managed disk will be copied to targetResourceGroupName=mytargetResourceGroupName #Set the context to the subscription Id where managed disk will be copied to az account set --subscription $targetSubscriptionId #Copy managed disk to different subscription using managed disk Id az disk create --resource-group $targetResourceGroupName --name $managedDiskName --source $managedDiskId
This script uses following commands to create a new managed disk in the target subscription using the Id of the source managed disk. Each command in the table links to command specific documentation.
|az disk show||Gets all the properties of a managed disk using the name and resource group properties of the managed disk. Id property is used to copy the managed disk to different subscription.|
|az disk create||Copies a managed disk by creating a new managed disk in different subscription using Id and name the parent managed disk.|
For more information on the Azure CLI, see Azure CLI documentation.
Additional virtual machine and managed disks CLI script samples can be found in the Azure Linux VM documentation.