将 VM 移到其他订阅或资源组Move a VM to another subscription or resource group

本文逐步说明如何在资源组或订阅之间移动虚拟机 (VM)。This article walks you through how to move a virtual machine (VM) between resource groups or subscriptions. 如果在个人订阅中创建了 VM,现在想要将其移到公司的订阅,则在订阅之间移动 VM 会很方便。Moving a VM between subscriptions can be handy if you created a VM in a personal subscription and now want to move it to your company's subscription.

重要

在移动过程中会创建新的资源 ID。New resource IDs are created as part of the move. 移动 VM 后,需要更新工具和脚本以使用新的资源 ID。After the VM has been moved, you will need to update your tools and scripts to use the new resource IDs.

使用 Azure CLI 移动 VMUse the Azure CLI to move a VM

在使用 Azure CLI 移动 VM 之前,需确保源订阅和目标订阅存在于同一租户中。Before you can move your VM by using the Azure CLI, you need to make sure the source and destination subscriptions exist within the same tenant. 若要检查这两个订阅是否具有相同的租户 ID,请使用 az account show 命令。To check that both subscriptions have the same tenant ID, use az account show.

备注

请先运行 az cloud set -n AzureChinaCloud 更改云环境,然后才能在 Azure 中国中使用 Azure CLI。Before you can use Azure CLI in Azure China , please run az cloud set -n AzureChinaCloud first to change the cloud environment. 若要切换回 Azure 公有云,请再次运行 az cloud set -n AzureCloudIf you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

az account show --subscription mySourceSubscription --query tenantId
az account show --subscription myDestinationSubscription --query tenantId

如果源和目标订阅的租户 ID 不相同,则必须联系支持人员才能将资源移动到新租户。If the tenant IDs for the source and destination subscriptions are not the same, you must contact support to move the resources to a new tenant.

若要成功移动 VM,需要移动 VM 及其所有支持资源。To successfully move a VM, you need to move the VM and all its supporting resources. 使用 az resource list 命令列出资源组中的所有资源及其 ID。Use the az resource list command to list all the resources in a resource group and their IDs. 这有助于通过管道将此命令的输出发送到文件,以便将 ID 复制并粘贴到后续命令中。It helps to pipe the output of this command to a file so you can copy and paste the IDs into later commands.

az resource list --resource-group "mySourceResourceGroup" --query "[].{Id:id}" --output table

若要将 VM 及其资源移到其他资源组,请使用 az resource move 命令。To move a VM and its resources to another resource group, use az resource move. 以下示例说明如何移动 VM 及其所需的大多数通用资源。The following example shows how to move a VM and the most common resources it requires. 使用 -ids 参数,并针对要移动的资源传入逗号分隔的 ID 列表(不包含空格)。Use the -ids parameter and pass in a comma-separated list (without spaces) of IDs for the resources to move.

vm=/subscriptions/mySourceSubscriptionID/resourceGroups/mySourceResourceGroup/providers/Microsoft.Compute/virtualMachines/myVM
nic=/subscriptions/mySourceSubscriptionID/resourceGroups/mySourceResourceGroup/providers/Microsoft.Network/networkInterfaces/myNIC
nsg=/subscriptions/mySourceSubscriptionID/resourceGroups/mySourceResourceGroup/providers/Microsoft.Network/networkSecurityGroups/myNSG
pip=/subscriptions/mySourceSubscriptionID/resourceGroups/mySourceResourceGroup/providers/Microsoft.Network/publicIPAddresses/myPublicIPAddress
vnet=/subscriptions/mySourceSubscriptionID/resourceGroups/mySourceResourceGroup/providers/Microsoft.Network/virtualNetworks/myVNet
diag=/subscriptions/mySourceSubscriptionID/resourceGroups/mySourceResourceGroup/providers/Microsoft.Storage/storageAccounts/mydiagnosticstorageaccount
storage=/subscriptions/mySourceSubscriptionID/resourceGroups/mySourceResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageaccountname    

az resource move \
    --ids $vm,$nic,$nsg,$pip,$vnet,$storage,$diag \
    --destination-group "myDestinationResourceGroup"

如果要将 VM 及其资源移到其他订阅,请添加 --destination-subscriptionId 参数来指定目标订阅。If you want to move the VM and its resources to a different subscription, add the --destination-subscriptionId parameter to specify the destination subscription.

当系统要求你确认是否要移动指定的资源时,请输入 Y 进行确认。When you are asked to confirm that you want to move the specified resources, enter Y to confirm.

使用 Azure 门户将 VM 移动到其他订阅Use the Azure portal to move a VM to a different subscription

可以使用 Azure 门户将 VM 及其关联的资源移动到其他订阅。You can move a VM and its associated resources to a different subscription by using the Azure portal.

  1. 打开 Azure 门户Open the Azure portal.
  2. 单击“浏览” > “资源组”,并选择包含要移动的 VM 的资源组。Click Browse > Resource groups and select the resource group containing the VM that you would like to move.
  3. 在资源组页的顶部,选择“移动”,然后选择“移动到另一个订阅” 。At the top of the page for the resource group, select Move and then select Move to another subscription. 此时将打开“移动资源” 页。The Move resources page opens.
  4. 选择要移动的每个资源。Select each of the resources to move. 大多数情况下,应移动列出的所有相关资源。In most cases, you should move all of the related resources that are listed.
  5. 选择要将 VM 移到其中的订阅Select the Subscription where you want the VM to be moved.
  6. 选择一个现有“资源组”,或者输入名称以创建新的资源组 。Select an existing Resource group, or enter a name to have a new resource group created.
  7. 完成后,选择你了解将创建新的资源 ID 并且移动后需要将新 ID 用于 VM,然后单击“确定” 。When you are done, select that you understand that new resource IDs will be created and that the new IDs will need to be used with the VM after it is moved, and then select OK.

使用 Azure 门户将 VM 移动到其他资源组Use the Azure portal to move a VM to another resource group

可以使用 Azure 门户将 VM 及其关联的资源移动到其他资源组。You can move a VM and its associated resources to another resource group by using the Azure portal.

  1. 打开 Azure 门户Open the Azure portal.
  2. 单击“浏览” > “虚拟机”,并从列表中选择要移动的 VM。Click Browse > Virtual machines and select the VM you would like to move from the list.
  3. 在 VM 页中,选择资源组标签旁边的“更改” 。In the page for the VM, next to the label for resource group, select Change. 此时将打开“移动资源” 页。The Move resources page opens.
  4. 选择要移动的每个资源。Select each of the resources to move. 大多数情况下,应移动列出的所有相关资源。In most cases, you should move all of the related resources that are listed.
  5. 选择一个现有“资源组”,或者输入名称以创建新的资源组 。Select an existing Resource group, or enter a name to have a new resource group created.
  6. 完成后,选择你了解将创建新的资源 ID 并且移动后需要将新 ID 用于 VM,然后单击“确定” 。When you are done, select that you understand that new resource IDs will be created and that the new IDs will need to be used with the VM after it is moved, and then select OK.

后续步骤Next steps

可以在资源组和订阅之间移动许多不同类型的资源。You can move many different types of resources between resource groups and subscriptions. 有关详细信息,请参阅将资源移到新资源组或订阅For more information, see Move resources to a new resource group or subscription.