使用 PowerShell 在区域之间移动资源Move resources across regions in PowerShell

本文介绍如何使用 Azure 资源转移器中的 PowerShell 将 Azure 资源转移到其他 Azure 区域。This article explains how to move Azure resources to a different Azure region, using PowerShell in Azure Resource Mover.

在本教程中,你将了解如何执行以下操作:In this tutorial, you learn how to:

  • 查看先决条件和要求。Check prerequisites and requirements.
  • 设置移动集合。Set up the move collection.
  • 将资源添加到移动集合,并解析依赖项。Add resources to the move collection, and resolve dependencies.
  • 准备并移动源资源组。Prepare and move the source resource group.
  • 准备并移动其他资源。Prepare and move the other resources.
  • 确定是放弃还是提交移动。Decide whether you want to discard or commit the move.
  • 移动后,可以选择删除源区域中的资源。Optionally remove resources in the source region after the move.

备注

教程显示尝试方案的最快路径,并使用默认选项。Tutorials show the quickest path for trying out a scenario, and use default options.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin. 然后登录到 Azure 门户Then sign in to the Azure portal.

先决条件Prerequisites

要求Requirement 说明Description
订阅权限Subscription permissions 请检查你对包含待移动资源的订阅是否具有“所有者”访问权限Check you have Owner access on the subscription containing the resources that you want to move

为什么需要“所有者”访问权限?Why do I need Owner access? 首次为 Azure 订阅中的特定源和目标对添加资源时,资源转移器将创建受订阅信任的系统分配的托管标识(以前称为托管服务标识 (MSI))。The first time you add a resource for a specific source and destination pair in an Azure subscription, Resource Mover creates a system-assigned managed identity (formerly known as Managed Service Identify (MSI)) that's trusted by the subscription. 若要创建标识,并为其分配所需的角色(来源订阅中的参与者或用户访问管理员),用于添加资源的帐户需要对订阅的“所有者”权限。To create the identity, and to assign it the required role (Contributor or User Access administrator in the source subscription), the account you use to add resources needs Owner permissions on the subscription. 详细了解 Azure 角色。Learn more about Azure roles.
资源移动器支持Resource Mover support 查看支持的区域和其他常见问题。Review supported regions and other common questions.
VM 支持VM support 检查要移动的 VM 是否受支持。Check that any VMs you want to move are supported.

- 验证受支持的 Windows VM。- Verify supported Windows VMs.

- 验证受支持的 Linux VM 和内核版本。- Verify supported Linux VMs and kernel versions.

- 检查受支持的计算存储网络设置。- Check supported compute, storage, and networking settings.
SQL 支持SQL support 如果想要移动 SQL 资源,请查看 SQL 要求列表If you want to move SQL resources, review the SQL requirements list.
目标订阅Destination subscription 目标区域中的订阅需要足够的配额来创建要在目标区域中移动的资源。The subscription in the destination region needs enough quota to create the resources you're moving in the target region. 如果没有配额,请请求其他限制If it doesn't have quota, request additional limits.
目标区域费用Destination region charges 验证与要将 VM 移动到的目标区域关联的定价和费用。Verify pricing and charges associated with the target region to which you're moving VMs. 请使用定价计算器来帮助你。Use the pricing calculator to help you.

查看 PowerShell 要求Review PowerShell requirements

无论是使用 Azure 门户还是 PowerShell,大多数移动资源操作是相同的,只有几个例外。Most move resources operations are the same whether using the Azure portal or PowerShell, with a couple of exceptions.

操作Operation PowerShellPowerShell PortalPortal
创建移动集合Create a move collection 移动集合(要移动的所有资源的列表)会自动创建。A move collection (a list of all the resources you're moving) is created automatically. 所需的标识权限会由门户在后端中分配。Required identity permissions are assigned in the backend by the portal. 使用 PowerShell cmdlet 执行以下操作:You use PowerShell cmdlets to:

- 为移动集合创建资源组,并指定其位置。- Create a resource group for the move collection and specify the location for it.

- 为集合分配托管标识。- Assign a managed identity to the collection.

- 为集合添加资源。- Add resources to the collection.
删除移动集合Remove a move collection 无法直接在门户中删除移动集合。You can't directly remove a move collection in the portal. 使用 PowerShell cmdlet 删除移动集合。You use a PowerShell cmdlet to remove a move collection.
资源移动操作Resource move operations

(准备、启动移动和提交等)。(Prepare, initiate move, commit etc.).
资源移动器自动验证的单个步骤。Single steps with automatic validation by Resource Mover. PowerShell cmdlet 可以:PowerShell cmdlets to:

1) 验证依赖项。1) Validate dependencies.

2) 执行移动。2) Perform the move.
删除源资源Delete source resources 直接在资源移动器门户中。Directly in the Resource Mover portal. 资源类型级别的 PowerShell cmdlet。PowerShell cmdlets at the resource-type level.

示例值Sample values

在脚本示例中使用以下值:We're using these values in our script examples:

设置Setting Value
订阅 IDSubscription ID subscription-idsubscription-id
源区域Source region 中国东部China East
目标区域Target region 中国北部China North
资源组(为移动集合保留元数据)Resource group (holding metadata for move collection) RG-MoveCollection-demoRMSRG-MoveCollection-demoRMS
移动集合名称Move collection name PS-chinaeast-chinanorth-demoRMS
资源组(源区域)Resource group (source region) PSDemoRMPSDemoRM
资源组(目标区域)Resource group (target region) PSDemoRM-targetPSDemoRM-target
资源移动服务位置Resource Move service location 中国东部 2China East 2
IdentityTypeIdentityType SystemAssignedSystemAssigned
要移动的 VMVM to move PSDemoVMPSDemoVM

登录 AzureSign into Azure

使用 Connect-AzAccount cmdlet 登录到 Azure 订阅:Sign into your Azure subscription with the Connect-AzAccount cmdlet:

Connect-AzAccount – Subscription "<subscription-id>"

设置移动集合Set up the move collection

MoveCollection 对象存储有关要移动的资源的元数据和配置信息。The MoveCollection object stores metadata and configuration information about resources you want to move. 若要设置移动集合,请执行以下操作:To set up a move collection, you do the following:

  • 为移动集合创建资源组。Create a resource group for the move collection.
  • 将服务提供程序注册到订阅,以便可以创建 MoveCollection 资源。Register the service provider to the subscription, so that the MoveCollection resource can be created.
  • 创建具有托管标识的 MoveCollection 对象。Create the MoveCollection object with managed identity. 为使 MoveCollection 对象能够访问资源转移器服务所在的订阅,它需要一个受订阅信任的系统分配的托管标识(以前称为托管服务标识,MSI)。For the MoveCollection object to access the subscription in which the Resource Mover service is located, it needs a system-assigned managed identity (formerly known as Managed Service Identity (MSI)) that's trusted by the subscription.
  • 为托管标识授予访问资源移动器订阅的权限。Grant access to the Resource Mover subscription for the managed identity.

创建资源组Create the resource group

为移动集合元数据和配置信息创建资源组,如下所示:Create a resource group for the move collection metadata and configuration information, as follows:

New-AzResourceGroup -Name "RG-MoveCollection-demoRMS" -Location "China East 2"

输出Output:

创建资源组后的输出文本

注册资源提供程序Register the resource provider

  1. 注册资源提供程序 Microsoft.Migrate,以便可以创建 MoveCollection 资源,如下所示:Register the resource provider Microsoft.Migrate, so that the MoveCollection resource can be created, as follows:

    Register-AzResourceProvider -ProviderNamespace Microsoft.Migrate
    
    
  2. Wait for registration:

    While(((Get-AzResourceProvider -ProviderNamespace Microsoft.Migrate)| where {$_.RegistrationState -eq "Registered" -and $_.ResourceTypes.ResourceTypeName -eq "moveCollections"}|measure).Count -eq 0)
    {
        Start-Sleep -Seconds 5
        Write-Output "Waiting for registration to complete."
    }
    

创建 MoveCollection 对象Create a MoveCollection object

创建 MoveCollection 对象,并为其分配托管标识,如下所示:Create a MoveCollection object, and assign a managed identity to it, as follows:

New-AzResourceMoverMoveCollection -Name "PS-chinaeast-chinanorth-demoRMS"  -ResourceGroupName "RG-MoveCollection-demoRMS" -SourceRegion "centralus" -TargetRegion "westcentralus" -Location "centraluseuap" -IdentityType "SystemAssigned"

输出Output:

创建移动集合后的输出文本

为托管标识授予访问权限Grant access to the managed identity

为托管标识授予访问资源移动器订阅的权限,如下所示。Grant the managed identity access to the Resource Mover subscription as follows. 你必须是订阅所有者。You must be the subscription owner.

  1. 检索 MoveCollection 对象的标识详细信息。Retrieve identity details from the MoveCollection object.

    $moveCollection = Get-AzResourceMoverMoveCollection -SubscriptionId $subscriptionId -ResourceGroupName "RG-MoveCollection-demoRMS" -Name "PS-chinaeast-chinanorth-demoRMS"
    
    $identityPrincipalId = $moveCollection.IdentityPrincipalId   
    
  2. 为标识分配所需角色,以便 Azure 资源移动器可以访问你的订阅,以帮助移动资源。Assign the required roles to the identity so Azure Resource Mover can access your subscription to help move resources.

    New-AzRoleAssignment -ObjectId $identityPrincipalId -RoleDefinitionName Contributor -Scope "/subscriptions/$subscriptionId"
    
    New-AzRoleAssignment -ObjectId $identityPrincipalId -RoleDefinitionName "User Access Administrator" -Scope "/subscriptions/$subscriptionId"
    

将资源添加到移动集合Add resources to the move collection

检索想要移动的现有源资源的 ID。Retrieve the IDs for existing source resources you want to move. 创建目标资源设置对象,然后将资源添加到移动集合。Create the destination resource settings object, then add resources to the move collection.

备注

添加到移动集合的资源必须在同一订阅中,但可以在不同的资源组中。Resources added to a move collection must be in the same subscription, but can be in different resource groups.

添加资源,如下所示:Add resources as follows:

  1. 获取源资源 ID:Get the source resource ID:

    Get-AzResource -Name PSDemoVM -ResourceGroupName PSDemoRM
    

    输出Output

    检索资源 ID 后的输出文本

  2. 根据要移动的资源,创建目标资源设置对象。Create the target resource settings object in accordance with the resource you're moving. 在本例中,它是一个 VM。In our case it's a VM.

    $targetResourceSettingsObj = New-Object Microsoft.Azure.PowerShell.Cmdlets.ResourceMover.Models.Api202101.VirtualMachineResourceSettings
    
  3. 为对象设置资源类型和目标资源名称。Set the resource type and target resource name for the object.

    $targetResourceSettingsObj.ResourceType = "Microsoft.Compute/virtualMachines"
    $targetResourceSettingsObj.TargetResourceName = "PSDemoVM"
    

    备注

    我们的目标 VM 与源区域中的 VM 具有相同的名称。Our target VM has the same name as the VM in the source region. 你可以选择其他名称。You can choose a different name.

  4. 使用已检索/创建的资源 ID 和目标设置对象,将源资源添加到移动集合。Add the source resources to the move collection, using the resource ID and target settings object you retrieved/created.

    Add-AzResourceMoverMoveResource -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS" -SourceId "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx xxxxxxxxxxxx/resourceGroups/
    PSDemoRM/providers/Microsoft.Compute/virtualMachines/PSDemoVM" -Name "PSDemoVM" -ResourceSetting $targetResourceSettingsObj
    

    输出 添加资源后输出文本Output Output text after adding the resource

验证并添加依赖项Validate and add dependencies

检查已添加的资源是否对其他资源具有依赖性,并根据需要添加。Check whether the resources you added have any dependencies on other resources, and add as needed.

  1. 验证依赖项,如下所示:Validate dependencies as follows:

    Resolve-AzResourceMoverMoveCollectionDependency -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS"
    

    输出(如果依赖项存在)Output (when dependencies exist)

    验证依赖项后输出文本

  2. 缺少依赖项的标识:Identity missing dependencies:

    • 若要检索所有缺失的依赖项的列表:To retrieve a list of all missing dependencies:

      Get-AzResourceMoverUnresolvedDependency -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS" -ResourceGroupName "RG-MoveCollection-demoRMS" -DependencyLevel Descendant"
      

      输出  检索所有依赖项的列表后输出文本Output Output text after retrieving a list of all dependencies

    • 若要仅检索第一级别的依赖项(资源的直接依赖项):To retrieve only first-level dependencies (direct dependencies for the resource):

      Get-AzResourceMoverUnresolvedDependency -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS" -ResourceGroupName "RG-MoveCollection-demoRMS" -DependencyLevel Direct
      

      输出  检索第一级别依赖项的列表后输出文本Output Output text after retrieving a list of first-level dependencies

  3. 若要添加任何未完成的缺失的依赖项,请重复上述说明,将资源添加到移动集合,并重新验证,直到没有未完成的资源。To add any outstanding missing dependencies, repeat the instructions above to add resources to the move collection, and revalidate until there are no outstanding resources.

备注

如果出于任何原因,你需要从资源集合删除资源,请遵循本文中的说明。If for any reason you want to remove resources from the resource collection, follow the instructions in this article.

添加源资源组Add the source resource group

添加源资源组,该资源组包含你想要移动到移动集合的资源。Add the source resource group that contains resources you want to move to the move collection.

  1. 检索资源组的 ID。Retrieve the ID of the resource group.

    Get-AzResourceMoverUnresolvedDependency -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS" -ResourceGroupName "RG-MoveCollection-demoRMS" -DependencyLevel Direct
    

    输出  检索资源组的 ID 后输出文本Output Output text after retrieving the ID of the source resource group

    备注

    我们将使用已在目标区域中的资源组。We're using a resource group that's already in the target region.

  2. 使用检索的 ID 将资源组添加到集合。Use the retrieved ID to add the resource group to the collection.

    Add-AzResourceMoverMoveResource -ResourceGroupName "RG-MoveCollection-demoRMS"  -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS" -SourceId "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/psdemorm"  -Name "psdemorm"  -ExistingTargetId "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/PSDemoRM-target"
    

    输出 将源资源组添加到移动集合后输出文本Output Output text after adding the source resource group to the move collection

  3. 验证依赖项,以确保在添加资源组后没有缺少任何依赖项。Verify dependencies to make sure you haven't missed anything after adding the resource group.

    Resolve-AzResourceMoverMoveCollectionDependency -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS"
    
  4. 我们发现不存在未完成的依赖项。We see there are no outstanding dependencies.

    输出 检查依赖项后输出文本Output Output text after checking dependencies

准备资源Prepare resources

在移动之前,通常需要在源区域中准备资源。You usually need to prepare resources in the source region before the move. 例如:For example:

  • 若要移动 Azure 虚拟网络、网络适配器、负载均衡器和网络安全组等无状态资源,可能需要导出 Azure 资源管理器模板。To move stateless resources such as Azure virtual networks, network adapters, load balancers, and network security groups, you might need to export an Azure Resource Manager template.
  • 若要移动 Azure VM 和 SQL 数据库等有状态的资源,可能需要开始将资源从源复制到目标区域。To move stateful resources such as Azure VMs and SQL databases, you might need to start replicating resources from the source to destination region.

在本教程中,由于我们要移动 VM,因此在开始准备 VM 前,我们需要准备源资源组,并且启用并提交其移动。In this tutorial, since we're moving VMs, we need to prepare the source resource group, and then initiate and commit its move, before we can start preparing VMs.

备注

如果你有现有的目标资源组,则可以直接为源资源组提交移动,并跳过准备和启动移动阶段。If you have an existing target resource group, you can directly commit the move for the source resource group, and skip the prepare and initiate move stages.

准备源资源组:Prepare the source resource group:

  1. 准备资源组:Prepare the resource group:

    Invoke-AzResourceMoverPrepare -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS"  -MoveResource “PSDemoRM”
    

    输出Output

    准备源资源组后的输出文本

  2. 启动源资源组的移动。Initiate the move of the source resource group.

    "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS"  -MoveResource “PSDemoRM”
    

    启动源资源组的移动后输出文本

  3. 为源资源组提交移动。Commit the move for the source resource group.

    Invoke-AzResourceMoverCommit -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS"  -MoveResource “PSDemoRM”
    

    输出Output

    提交源资源组后输出文本

准备 VM 资源Prepare VM resources

准备并移动源资源组后,我们可以为移动准备 VM 资源。After preparing and moving the source resource group, we can prepare VM resources for the move.

  1. 在准备 VM 资源前,验证依赖项。Validate the dependencies before you prepare VM resources.

    $resp = Invoke-AzResourceMoverPrepare -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS"  -MoveResource $('psdemovm') -ValidateOnly
    

    输出Output

    在准备 VM 之前且验证 VM 后输出文本

  2. 获取需要与 VM 一起准备的依赖性资源。Get the dependent resources that need to be prepared along with the VM.

    $resp.AdditionalInfo[0].InfoMoveResource
    

    输出Output

    检索依赖性 VM 资源后输出文本

  3. 为所有依赖性资源启动准备过程。Initiate the prepare process for all dependent resources.

    Invoke-AzResourceMoverPrepare -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS"  -MoveResource $('PSDemoVM','psdemovm111', 'PSDemoRM-vnet','PSDemoVM-nsg')
    

    输出Output

    启动所有资源的准备后输出文本

    备注

    可以提供源资源 ID(而不是资源名称)作为准备 cmdlet 的输入参数,在启动移动和提交 cmdlet 中同样如此。You can provide the source resource ID instead of resource name as the input parameters for the Prepare cmdlet, as well as in the Initiate Move and Commit cmdlets. 若要执行此操作,请运行:To do this, run:

        Invoke-AzResourceMoverPrepare -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS" -MoveResourceInputType MoveResourceSourceId  -MoveResource $('/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/PSDemoRMS/providers/Microsoft.Network/networkSecurityGroups/PSDemoVM-nsg')
    

启动 VM 资源的移动Initiate move of VM resources

  1. 验证 VM 资源是否处于启动移动挂起状态:Verify that the VM resources are in an Initiate Move Pending state:

    Get-AzResourceMoverMoveResource  -SubscriptionId “ xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx “ -ResourceGroupName “RG-MoveCollection-demoRMS” -MoveCollectionName “PS-chinaeast-chinanorth-demoRMS ”   | Where-Object {  $_.MoveStatusMoveState -eq “InitiateMovePending” } | Select Name
    

    输出Output

    检查启动状态后输出文本

  2. 启动移动:Initiate the move:

    Invoke-AzResourceMoverInitiateMove -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS"  -MoveResource $('psdemovm111', 'PSDemoRM-vnet','PSDemoVM-nsg', ‘PSDemoVM’) -MoveResourceInputType "MoveResourceId"
    

    输出Output

    启动资源的移动后输出文本

放弃还是提交?Discard or commit?

初始移动后,你可以决定是提交移动,还是放弃它。After the initial move, you can decide whether you want to commit the move, or to discard it.

  • 放弃:如果要测试,并且不想实际移动源资源,则可能需要放弃移动。Discard: You might discard a move if you're testing, and you don't want to actually move the source resource. 放弃移动会将资源返回到“启动移动挂起”状态。Discarding the move returns the resource to a state of Initiate move pending. 然后可以根据需要再次启动移动。You can then initiate the move again if needed.
  • 提交:提交即完成移动到目标区域的操作。Commit: Commit completes the move to the target region. 提交后,源资源将处于“删除源挂起”状态,你可以决定是否要删除它。After committing, a source resource will be in a state of Delete source pending, and you can decide if you want to delete it.

放弃移动Discard the move

放弃移动:To discard the move:

Invoke-AzResourceMoverDiscard -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS"  -MoveResource $('psdemovm111', 'PSDemoRM-vnet','PSDemoVM-nsg', ‘PSDemoVM’) -MoveResourceInputType "MoveResourceId"

输出Output

放弃移动后的输出文本

提交移动Commit the move

  1. 提交移动,如下所示:Commit the move as follows:

    Invoke-AzResourceMoverCommit -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-chinaeast-chinanorth-demoRMS"  -MoveResource $('psdemovm111', 'PSDemoRM-vnet','PSDemoVM-nsg', ‘PSDemoVM’) -MoveResourceInputType "MoveResourceId"
    

    输出Output

    提交移动后的输出文本

  2. 验证是否所有资源都已移动到目标区域:Verify that all resources have moved to the target region:

    Get-AzResourceMoverMoveResource  -ResourceGroupName “RG-MoveCollection-demoRMS ” -MoveCollectionName “PS-chinaeast-chinanorth-demoRMS”   
    

    所有资源在目标区域中现在都处于“删除源挂起”状态。All resources are now in a Delete Source Pending state in the target region.

删除源资源Delete source resources

提交移动并验证资源可在目标区域中按预期工作后,可以使用 PowerShellAzure CLI 删除 Azure 门户中的每个源资源。After committing the move, and verifying that resources work as expected in the target region, you can delete each source resource in the Azure portal, using PowerShell, or Azure CLI.

后续步骤Next steps

在本教程中,你将了解:In this tutorial, you:

  • 使用 PowerShell 将 Azure VM 移动到了其他 Azure 区域。Moved Azure VMs to another Azure region using PowerShell.
  • 将与 VM 关联的资源移动到其他区域。Moved resources associated with VMs to another region.

现在,尝试使用门户移动 Azure VMNow, trying moving Azure VMs using the portal