使用 PowerShell 将 IaaS 资源从经典部署模型迁移到 Azure 资源管理器部署模型Migrate IaaS resources from classic to Azure Resource Manager by using PowerShell

重要

目前,大约有 90% 的 IaaS VM 在使用 Azure 资源管理器Today, about 90% of IaaS VMs are using Azure Resource Manager. 自 2020 年 2 月 28 日起,经典 VM 已弃用,并将于 2023 年 3 月 1 日完全停用。As of February 28, 2020, classic VMs have been deprecated and will be fully retired on March 1, 2023. 详细了解此弃用以及它对你的影响Learn more about this deprecation and how it affects you.

以下步骤演示了如何使用 Azure PowerShell 命令将基础结构即服务 (IaaS) 资源从经典部署模型迁移到 Azure Resource Manager 部署模型。These steps show you how to use Azure PowerShell commands to migrate infrastructure as a service (IaaS) resources from the classic deployment model to the Azure Resource Manager deployment model.

也可以根据需要使用 Azure CLI 迁移资源。If you want, you can also migrate resources by using the Azure CLI.


在以下流程图中确定在迁移过程中需要执行步骤的顺序。Here's a flowchart to identify the order in which steps need to be executed during a migration process.

Screenshot that shows the migration steps

步骤 1:规划迁移Step 1: Plan for migration

下面是在评估是否将 IaaS 资源从经典部署模型迁移到资源管理器部署模型时的一些建议最佳做法:Here are a few best practices that we recommend as you evaluate whether to migrate IaaS resources from classic to Resource Manager:

  • 通读受支持的和不受支持的功能和配置Read through the supported and unsupported features and configurations. 如果虚拟机使用不受支持的配置或功能,请等到我们宣布支持该配置或功能时再进行迁移。If you have virtual machines that use unsupported configurations or features, wait for the configuration or feature support to be announced. 也可根据需要删除该功能或移出该配置,以利迁移进行。Alternatively, if it suits your needs, remove that feature or move out of that configuration to enable migration.
  • 如果通过自动化脚本来部署目前的基础结构和应用程序,则可尝试使用这些脚本进行迁移,以便创建类似的测试性设置。If you have automated scripts that deploy your infrastructure and applications today, try to create a similar test setup by using those scripts for migration. 也可以使用 Azure 门户设置示例环境。Alternatively, you can set up sample environments by using the Azure portal.

重要

目前不支持通过应用程序网关从经典部署模型迁移到资源管理器部署模型。Application gateways aren't currently supported for migration from classic to Resource Manager. 若要迁移带应用程序网关的虚拟网络,请先删除该网关,然后运行“准备”操作来移动网络。To migrate a virtual network with an application gateway, remove the gateway before you run a Prepare operation to move the network. 完成迁移后,在 Azure Resource Manager 中重新连接该网关。After you complete the migration, reconnect the gateway in Azure Resource Manager.

无法自动迁移其他订阅中连接到 ExpressRoute 线路的 Azure ExpressRoute 网关。Azure ExpressRoute gateways that connect to ExpressRoute circuits in another subscription can't be migrated automatically. 此类情况下,请删除 ExpressRoute 网关、迁移虚拟网络并重新创建网关。In such cases, remove the ExpressRoute gateway, migrate the virtual network, and re-create the gateway. 有关详细信息,请参阅将 ExpressRoute 线路和关联的虚拟网络从经典部署模型迁移到资源管理器部署模型For more information, see Migrate ExpressRoute circuits and associated virtual networks from the classic to the Resource Manager deployment model.

步骤 2:安装最新版本的 PowerShellStep 2: Install the latest version of PowerShell

有两个主要选项可供安装 Azure PowerShell:PowerShell 库Web 平台安装程序 (WebPI)There are two main options to install Azure PowerShell: PowerShell Gallery or Web Platform Installer (WebPI). WebPI 接收每月的更新。WebPI receives monthly updates. PowerShell 库会持续接收更新。PowerShell Gallery receives updates on a continuous basis. 本文基于 Azure PowerShell 2.1.0 版。This article is based on Azure PowerShell version 2.1.0.

如需安装说明,请参阅 How to install and configure Azure PowerShell(如何安装和配置 Azure PowerShell)。For installation instructions, see How to install and configure Azure PowerShell.

步骤 3:确保你是订阅的管理员Step 3: Ensure that you're an administrator for the subscription

若要执行此迁移,必须在 Azure 门户中将你添加为订阅的共同管理员。To perform this migration, you must be added as a coadministrator for the subscription in the Azure portal.

  1. 登录到 Azure 门户Sign in to the Azure portal.
  2. 在“中心”菜单上中选择“订阅”。 On the Hub menu, select Subscription. 如果看不到该选项,请选择“所有服务”。If you don't see it, select All services.
  3. 查找相应订阅项,然后查看“我的角色”字段。Find the appropriate subscription entry, and then look at the MY ROLE field. 对于共同管理员,该值应为“帐户管理员”。For a coadministrator, the value should be Account admin.

如果无法添加协同管理员,请联系订阅的服务管理员或协同管理员,将自己添加为协同管理员。If you're not able to add a coadministrator, contact a service administrator or coadministrator for the subscription to get yourself added.

步骤 4:设置订阅并针对迁移进行注册Step 4: Set your subscription, and sign up for migration

首先,请启动 PowerShell 提示符。First, start a PowerShell prompt. 对于迁移,请针对经典部署模型和资源管理器部署模型设置环境。For migration, set up your environment for both classic and Resource Manager.

登录到 Resource Manager 模型的帐户。Sign in to your account for the Resource Manager model.

    Connect-AzAccount -Environment AzureChinaCloud

使用以下命令获取可用订阅:Get the available subscriptions by using the following command:

    Get-AzSubscription | Sort Name | Select Name

设置当前会话的 Azure 订阅。Set your Azure subscription for the current session. 此示例将默认订阅名称设置为 My Azure SubscriptionThis example sets the default subscription name to My Azure Subscription. 将示例订阅名称替换成自己的名称。Replace the example subscription name with your own.

    Select-AzSubscription -SubscriptionName "My Azure Subscription"

备注

注册是一次性步骤,但必须在尝试迁移之前完成。Registration is a one-time step, but you must do it once before you attempt migration. 如果不注册,则会出现以下错误消息:Without registering, you see the following error message:

BadRequest:Subscription is not registered for migration. (BadRequest:订阅尚未注册迁移。)BadRequest : Subscription is not registered for migration.

使用以下命令向迁移资源提供程序注册:Register with the migration resource provider by using the following command:

    Register-AzResourceProvider -ProviderNamespace Microsoft.ClassicInfrastructureMigrate

请等五分钟让注册完成。Wait five minutes for the registration to finish. 使用以下命令检查审批状态:Check the status of the approval by using the following command:

    Get-AzResourceProvider -ProviderNamespace Microsoft.ClassicInfrastructureMigrate

请确保在继续操作之前,RegistrationState 为 RegisteredMake sure that RegistrationState is Registered before you proceed.

在切换到经典部署模型之前,请确保在当前部署或虚拟网络的 Azure 区域中有足够的 Azure 资源管理器虚拟机 vCPU。Before switching to the classic deployment model, make sure that you have enough Azure Resource Manager virtual machine vCPUs in the Azure region of your current deployment or virtual network. 可以使用以下 PowerShell 命令检查 Azure 资源管理器中目前的 vCPU 数量。You can use the following PowerShell command to check the current number of vCPUs you have in Azure Resource Manager. 若要了解有关 vCPU 配额的详细信息,请参阅限制和 Azure 资源管理器To learn more about vCPU quotas, see Limits and the Azure Resource Manager.

此示例检查 中国北部 区域的可用性。This example checks the availability in the China North region. 使用自己的区域名称替换示例名称。Replace the example region name with your own.

    Get-AzVMUsage -Location "China North"

现在,请登录到经典部署模型的帐户。Now, sign in to your account for the classic deployment model.

    Add-AzureAccount -Environment AzureChinaCloud

使用以下命令获取可用订阅:Get the available subscriptions by using the following command:

    Get-AzureSubscription | Sort SubscriptionName | Select SubscriptionName

设置当前会话的 Azure 订阅。Set your Azure subscription for the current session. 此示例将默认订阅设置为 My Azure SubscriptionThis example sets the default subscription to My Azure Subscription. 将示例订阅名称替换成自己的名称。Replace the example subscription name with your own.

    Select-AzureSubscription -SubscriptionName "My Azure Subscription"

步骤 5:运行迁移 IaaS 资源的命令Step 5: Run commands to migrate your IaaS resources

备注

此处描述的所有操作都是幂等的。All the operations described here are idempotent. 如果遇到功能不受支持或配置错误以外的问题,建议重试准备、中止或提交操作。If you have a problem other than an unsupported feature or a configuration error, we recommend that you retry the prepare, abort, or commit operation. 然后,平台会尝试再次操作。The platform then tries the action again.

步骤 5.1:选项 1 - 迁移云服务中的虚拟机(不在虚拟网络中)Step 5.1: Option 1 - Migrate virtual machines in a cloud service (not in a virtual network)

使用以下命令获取云服务列表。Get the list of cloud services by using the following command. 然后选取要迁移的云服务。Then pick the cloud service that you want to migrate. 如果云服务中的 VM 在虚拟网络中或者具有 Web 角色或辅助角色,该命令会返回错误消息。If the VMs in the cloud service are in a virtual network or if they have web or worker roles, the command returns an error message.

    Get-AzureService | ft Servicename

获取云服务的部署名称。Get the deployment name for the cloud service. 在此示例中,服务名称是 My ServiceIn this example, the service name is My Service. 使用自己的服务名称替换示例名称。Replace the example service name with your own service name.

    $serviceName = "My Service"
    $deployment = Get-AzureDeployment -ServiceName $serviceName
    $deploymentName = $deployment.DeploymentName

准备迁移云服务中的虚拟机。Prepare the virtual machines in the cloud service for migration. 可以从两个选项中进行选择。You have two options to choose from.

  • 选项 1:将 VM 迁移到平台所创建的虚拟网络上。Option 1: Migrate the VMs to a platform-created virtual network.

    首先,使用以下命令验证用户是否可以迁移云服务:First, validate that you can migrate the cloud service by using the following commands:

    $validate = Move-AzureService -Validate -ServiceName $serviceName `
        -DeploymentName $deploymentName -CreateNewVirtualNetwork
    $validate.ValidationMessages
    

    以下命令会显示任何阻止迁移的警告和错误。The following command displays any warnings and errors that block migration. 如果验证成功,可继续执行“准备”步骤。If validation is successful, you can move on to the Prepare step.

    Move-AzureService -Prepare -ServiceName $serviceName `
        -DeploymentName $deploymentName -CreateNewVirtualNetwork
    
  • 选项 2:迁移到资源管理器部署模型中的现有虚拟网络。Option 2: Migrate to an existing virtual network in the Resource Manager deployment model.

    此示例将资源组名称设置为 myResourceGroup、将虚拟网络名称设置为 myVirtualNetwork 并将子网名称设置为 mySubNetThis example sets the resource group name to myResourceGroup, the virtual network name to myVirtualNetwork, and the subnet name to mySubNet. 将该示例中的名称替换成自己的资源名称。Replace the names in the example with the names of your own resources.

    $existingVnetRGName = "myResourceGroup"
    $vnetName = "myVirtualNetwork"
    $subnetName = "mySubNet"
    

    首先,使用以下命令验证用户是否可以迁移虚拟网络:First, validate that you can migrate the virtual network by using the following command:

    $validate = Move-AzureService -Validate -ServiceName $serviceName `
        -DeploymentName $deploymentName -UseExistingVirtualNetwork -VirtualNetworkResourceGroupName $existingVnetRGName -VirtualNetworkName $vnetName -SubnetName $subnetName
    $validate.ValidationMessages
    

    以下命令会显示任何阻止迁移的警告和错误。The following command displays any warnings and errors that block migration. 如果验证成功,则可继续执行以下准备步骤:If validation is successful, you can proceed with the following Prepare step:

        Move-AzureService -Prepare -ServiceName $serviceName -DeploymentName $deploymentName `
        -UseExistingVirtualNetwork -VirtualNetworkResourceGroupName $existingVnetRGName `
        -VirtualNetworkName $vnetName -SubnetName $subnetName
    

使用前述任一选项成功完成准备操作以后,即可查询 VM 的迁移状态。After the Prepare operation succeeds with either of the preceding options, query the migration state of the VMs. 确保 VM 处于 Prepared 状态。Ensure that they're in the Prepared state.

此示例将 VM 名称设置为 myVMThis example sets the VM name to myVM. 使用自己的 VM 名称替换示例名称。Replace the example name with your own VM name.

    $vmName = "myVM"
    $vm = Get-AzureVM -ServiceName $serviceName -Name $vmName
    $vm.VM.MigrationState

使用 PowerShell 或 Azure 门户查看准备就绪的资源的配置。Check the configuration for the prepared resources by using either PowerShell or the Azure portal. 如果尚未做好迁移准备,因此想要回到旧的状态,请使用以下命令:If you're not ready for migration and you want to go back to the old state, use the following command:

    Move-AzureService -Abort -ServiceName $serviceName -DeploymentName $deploymentName

如果准备好的配置看起来没问题,则可继续进行,使用以下命令提交资源:If the prepared configuration looks good, you can move forward and commit the resources by using the following command:

    Move-AzureService -Commit -ServiceName $serviceName -DeploymentName $deploymentName

步骤 5.1:选项 2 - 迁移虚拟网络中的虚拟机Step 5.1: Option 2 - Migrate virtual machines in a virtual network

若要迁移虚拟网络中的虚拟机,可迁移虚拟网络。To migrate virtual machines in a virtual network, you migrate the virtual network. 虚拟机随虚拟网络自动迁移。The virtual machines automatically migrate with the virtual network. 选取要迁移的虚拟网络。Pick the virtual network that you want to migrate.

备注

通过使用虚拟机的 VHD(OS 和数据)文件创建新的使用托管磁盘的资源管理器虚拟机,来迁移使用经典部署模型创建的单个虚拟机。Migrate a single virtual machine created using the classic deployment model by creating a new Resource Manager virtual machine with Managed Disks by using the VHD (OS and data) files of the virtual machine.

备注

虚拟网络名称可能和新门户中显示的名称不同。The virtual network name might be different from what is shown in the new portal. 新的 Azure 门户显示名称为 [vnet-name],但实际的虚拟网络名称的类型是 Group [resource-group-name] [vnet-name]The new Azure portal displays the name as [vnet-name], but the actual virtual network name is of type Group [resource-group-name] [vnet-name]. 在开始迁移之前,请使用命令 Get-AzureVnetSite | Select -Property Name 查找实际的虚拟网络名称,或者在旧版 Azure 门户中查看它。Before you start the migration, look up the actual virtual network name by using the command Get-AzureVnetSite | Select -Property Name or view it in the old Azure portal.

此示例将虚拟网络名称设置为 myVnetThis example sets the virtual network name to myVnet. 使用自己的虚拟网络名称替换示例名称。Replace the example virtual network name with your own.

    $vnetName = "myVnet"

备注

如果虚拟网络包含的 Web 角色/辅助角色或 VM 的配置不受支持,则会出现验证错误消息。If the virtual network contains web or worker roles, or VMs with unsupported configurations, you get a validation error message.

首先,使用以下命令验证用户是否可以迁移虚拟网络:First, validate that you can migrate the virtual network by using the following command:

    Move-AzureVirtualNetwork -Validate -VirtualNetworkName $vnetName

以下命令会显示任何阻止迁移的警告和错误。The following command displays any warnings and errors that block migration. 如果验证成功,则可继续执行以下准备步骤:If validation is successful, you can proceed with the following Prepare step:

    Move-AzureVirtualNetwork -Prepare -VirtualNetworkName $vnetName

使用 Azure PowerShell 或 Azure 门户查看已准备就绪的虚拟机的配置。Check the configuration for the prepared virtual machines by using either Azure PowerShell or the Azure portal. 如果尚未做好迁移准备,因此想要回到旧的状态,请使用以下命令:If you're not ready for migration and you want to go back to the old state, use the following command:

    Move-AzureVirtualNetwork -Abort -VirtualNetworkName $vnetName

如果准备好的配置看起来没问题,则可继续进行,使用以下命令提交资源:If the prepared configuration looks good, you can move forward and commit the resources by using the following command:

    Move-AzureVirtualNetwork -Commit -VirtualNetworkName $vnetName

步骤 5.2:迁移存储帐户Step 5.2: Migrate a storage account

完成虚拟机迁移之后,请先执行以下先决条件检查,然后再迁移存储帐户。After you're done migrating the virtual machines, perform the following prerequisite checks before you migrate the storage accounts.

备注

如果存储帐户没有关联的磁盘或 VM 数据,则可以直接跳至“验证存储帐户并开始迁移”部分。If your storage account has no associated disks or VM data, you can skip directly to the "Validate storage accounts and start migration" section.

  • 关于是否迁移了 VM 或存储帐户是否有磁盘资源的先决条件检查:Prerequisite checks if you migrated any VMs or your storage account has disk resources:

    • 迁移其磁盘存储在存储帐户中的虚拟机。Migrate virtual machines whose disks are stored in the storage account.

      以下命令返回存储帐户中所有 VM 磁盘的 RoleName 和 DiskName 属性。The following command returns RoleName and DiskName properties of all the VM disks in the storage account. RoleName 是磁盘附加到的虚拟机的名称。RoleName is the name of the virtual machine to which a disk is attached. 如果此命令返回了磁盘,请确保先迁移这些磁盘附加到的虚拟机,再迁移存储帐户。If this command returns disks, then ensure that virtual machines to which these disks are attached are migrated before you migrate the storage account.

       $storageAccountName = 'yourStorageAccountName'
        Get-AzureDisk | where-Object {$_.MediaLink.Host.Contains($storageAccountName)} | Select-Object -ExpandProperty AttachedTo -Property `
        DiskName | Format-List -Property RoleName, DiskName
      
      
    • 删除存储帐户中存储的未附加 VM 磁盘。Delete unattached VM disks stored in the storage account.

      使用以下命令查找存储帐户中未附加的 VM 磁盘:Find unattached VM disks in the storage account by using the following command:

          $storageAccountName = 'yourStorageAccountName'
          Get-AzureDisk | where-Object {$_.MediaLink.Host.Contains($storageAccountName)} | Where-Object -Property AttachedTo -EQ $null | Format-List -Property DiskName  
      
      

      如果上述命令返回了磁盘,请使用以下命令删除这些磁盘:If the previous command returns disks, delete these disks by using the following command:

         Remove-AzureDisk -DiskName 'yourDiskName'
      
    • 删除存储帐户中存储的 VM 映像。Delete VM images stored in the storage account.

      以下命令返回 OS 磁盘存储在存储帐户中的所有 VM 映像。The following command returns all the VM images with OS disks stored in the storage account.

         Get-AzureVmImage | Where-Object { $_.OSDiskConfiguration.MediaLink -ne $null -and $_.OSDiskConfiguration.MediaLink.Host.Contains($storageAccountName)`
                                 } | Select-Object -Property ImageName, ImageLabel
      

      以下命令返回数据磁盘存储在存储帐户中的所有 VM 映像。The following command returns all the VM images with data disks stored in the storage account.

      
         Get-AzureVmImage | Where-Object {$_.DataDiskConfigurations -ne $null `
                                          -and ($_.DataDiskConfigurations | Where-Object {$_.MediaLink -ne $null -and $_.MediaLink.Host.Contains($storageAccountName)}).Count -gt 0 `
                                         } | Select-Object -Property ImageName, ImageLabel
      

      使用以下命令删除上述命令返回的所有 VM 映像:Delete all the VM images returned by the previous commands by using this command:

      Remove-AzureVMImage -ImageName 'yourImageName'
      
  • 验证存储帐户并开始迁移。Validate storage accounts and start migration.

    使用以下命令验证要迁移的每个存储帐户。Validate each storage account for migration by using the following command. 在此示例中,存储帐户名称为 myStorageAccountIn this example, the storage account name is myStorageAccount. 使用自己的存储帐户名称替换示例名称。Replace the example name with the name of your own storage account.

        $storageAccountName = "myStorageAccount"
        Move-AzureStorageAccount -Validate -StorageAccountName $storageAccountName
    

    下一步是准备存储帐户以进行迁移。The next step is to prepare the storage account for migration.

        $storageAccountName = "myStorageAccount"
        Move-AzureStorageAccount -Prepare -StorageAccountName $storageAccountName
    

    使用 Azure PowerShell 或 Azure 门户检查准备就绪的存储帐户的配置。Check the configuration for the prepared storage account by using either Azure PowerShell or the Azure portal. 如果尚未做好迁移准备,因此想要回到旧的状态,请使用以下命令:If you're not ready for migration and you want to go back to the old state, use the following command:

        Move-AzureStorageAccount -Abort -StorageAccountName $storageAccountName
    

    如果准备好的配置看起来没问题,则可继续进行,使用以下命令提交资源:If the prepared configuration looks good, you can move forward and commit the resources by using the following command:

        Move-AzureStorageAccount -Commit -StorageAccountName $storageAccountName
    

后续步骤Next steps