使用 CLI 将托管磁盘复制到相同或不同的订阅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. 仅当订阅属于同一 Azure AD 租户时,复制才适用。The copy works only when the subscriptions are part of the same Azure AD tenant.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅If you don't have an Azure trail subscription, create a trial subscription before you begin.

示例脚本Sample script

#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

脚本说明Script explanation

此脚本使用以下命令,通过源托管磁盘的 ID 在目标订阅中创建新的托管磁盘。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.

命令Command 说明Notes
az disk showaz disk show 使用托管磁盘的名称和资源组属性获取该托管磁盘的所有属性。Gets all the properties of a managed disk using the name and resource group properties of the managed disk. 使用 ID 属性将托管磁盘复制到其他订阅。Id property is used to copy the managed disk to different subscription.
az disk createaz disk create 通过使用父托管磁盘的 ID 和名称在不同订阅中创建新的托管磁盘来复制该托管磁盘。Copies a managed disk by creating a new managed disk in different subscription using Id and name the parent managed disk.

后续步骤Next steps

从托管磁盘创建虚拟机Create a virtual machine from a managed disk

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure Linux VM 文档中找到其他虚拟机和托管磁盘 CLI 脚本示例。Additional virtual machine and managed disks CLI script samples can be found in the Azure Linux VM documentation.