您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

使用 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 CLITo run this sample, make sure you have installed the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

此示例在 Bash shell 中正常工作。This sample works in a Bash shell. 有关在 Windows 客户端上运行 Azure CLI 脚本的选项,请参阅在 Windows 上安装 Azure CLIFor options on running Azure CLI scripts on Windows client, see Install the Azure CLI on Windows.

如果没有 Azure 订阅,请在开始之前创建一个免费帐户If you don't have an Azure subscription, create a free account 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.