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

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

以下步骤演示了如何使用 Azure PowerShell 命令将基础结构即服务 (IaaS) 资源从经典部署模型迁移到 Azure 资源管理器部署模型。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 命令行接口 (Azure CLI) 迁移资源。If you want, you can also migrate resources by using the Azure Command Line Interface (Azure CLI).


下面是一个流程图,用于确定在迁移过程中需要执行步骤的顺序Here is 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 资源从经典部署模型迁移到 Resource Manager 部署模型时遵循的一些最佳实践:Here are a few best practices that we recommend as you evaluate migrating 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, we recommend that you wait for the configuration/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.

重要

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

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

步骤2:安装最新版本的 Azure PowerShellStep 2: Install the latest version of Azure 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.

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


步骤 3:确保你是 Azure 门户中订阅的管理员Step 3: Ensure that you are an administrator for the subscription in Azure portal

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

  1. 登录到 Azure 门户Sign into 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, then look at the MY ROLE field. 对于协同管理员,该值应为“帐户管理员”。For a co-administrator, the value should be Account admin.

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

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

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

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

    Login-AzureRmAccount

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

    Get-AzureRMSubscription | Sort Name | Select Name

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

    Select-AzureRmSubscription –SubscriptionName "My Azure Subscription"

备注

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

BadRequest : Subscription is not registered for migration.BadRequest : Subscription is not registered for migration.

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

    Register-AzureRmResourceProvider -ProviderNamespace Microsoft.ClassicInfrastructureMigrate

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

    Get-AzureRmResourceProvider -ProviderNamespace Microsoft.ClassicInfrastructureMigrate

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

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

    Add-AzureAccount

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

    Get-AzureSubscription | Sort SubscriptionName | Select SubscriptionName

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

    Select-AzureSubscription –SubscriptionName "My Azure Subscription"


步骤 5:确保在当前部署或 VNET 的 Azure 区域中有足够的 Azure 资源管理器虚拟机 vCPUStep 5: Make sure you have enough Azure Resource Manager Virtual Machine vCPUs in the Azure region of your current deployment or VNET

可以使用以下 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 West US region. 将示例区域名称替换成自己的名称。Replace the example region name with your own.

Get-AzureRmVMUsage -Location "West US"

步骤 6:运行迁移 IaaS 资源的命令Step 6: 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.

步骤 6.1:选项 1 - 迁移云服务中的虚拟机(不在虚拟网络中)Step 6.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, and 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. 在此示例中,服务名称是“我的服务”。In 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 if you can migrate the cloud service using the following commands:

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

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

    Move-AzureService -Prepare -ServiceName $serviceName `
        -DeploymentName $deploymentName -CreateNewVirtualNetwork
    
  • 选项 2.迁移到 Resource Manager 部署模型中的现有虚拟网络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 if you can migrate the virtual network using the following command:

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

    前一个命令会显示任何阻止迁移的警告和错误。The preceding command displays any warnings and errors that block migration. 如果验证成功,则可继续执行以下准备步骤:If validation is successful, then 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 are 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 are 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

步骤 6.1:选项 2 - 迁移虚拟网络中的虚拟机Step 6.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 和数据)文件创建新的使用托管磁盘的 Resource Manager 虚拟机来迁移单个经典虚拟机Migrate single classic virtual machine by creating a new Resource Manager virtual machine with Managed Disks 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 migrating, lookup the actual virtual network name 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 if you can migrate the virtual network by using the following command:

    Move-AzureVirtualNetwork -Validate -VirtualNetworkName $vnetName

前一个命令会显示任何阻止迁移的警告和错误。The preceding command displays any warnings and errors that block migration. 如果验证成功,则可继续执行以下准备步骤:If validation is successful, then 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 are 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

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

完成虚拟机迁移之后,建议迁移存储帐户。Once you're done migrating the virtual machines, we recommend you migrate the storage accounts.

在迁移存储帐户之前,请执行以下先决条件检查:Before you migrate the storage account, please perform preceding prerequisite checks:

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

    上述命令返回存储帐户中所有经典 VM 磁盘的 RoleName 和 DiskName 属性。Preceding command returns RoleName and DiskName properties of all the classic VM disks in the storage account. RoleName 是磁盘附加到的虚拟机的名称。RoleName is the name of the virtual machine to which a disk is attached. 如果上述命令返回了磁盘,请确保先迁移这些磁盘所附加到的虚拟机,再迁移存储帐户。If preceding command returns disks then ensure that virtual machines to which these disks are attached are migrated before migrating 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 classic VM disks stored in the storage account

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

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

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

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

    上述命令返回 OS 磁盘存储在该存储帐户中的所有 VM 映像。Preceding command returns all the VM images with OS disk 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 映像。Preceding 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 above commands using preceding command:

    Remove-AzureVMImage -ImageName 'yourImageName'
    

使用以下命令验证要迁移的每个存储帐户。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

下一步是准备要迁移的存储帐户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 are 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