共用方式為


在 PowerShell 中跨區域移動資源

本文說明如何使用 Azure Resource Mover 中的 PowerShell,將 Azure 資源移至不同的 Azure 區域。

在本教學課程中,您會了解如何:

  • 將 Azure 資源移至不同的 Azure 區域

注意

教學課程顯示試用案例的最快路徑,並使用預設選項。

必要條件

確認下列需求:

需求 描述
訂用帳戶許可權 檢查您擁有 訂用帳戶的擁有者 存取權,其中包含您想要移動的資源。

第一次在 Azure 訂用帳戶中為特定來源和目的地配對新增資源時, 必須有系統指派的受控識別 (先前稱為受控服務識別(MSI)。訂用帳戶信任的資源。 若要建立身分識別,並為其指派必要的角色(來源訂用帳戶中的參與者或使用者存取系統管理員),您用來新增資源的帳戶需要 訂用帳戶的擁有者 許可權。 深入瞭解 Azure 角色。
資源移動器支援 檢閱 支援的區域和其他常見問題。
VM 支援 檢查是否支援您想要移動的任何 VM。

- 確認 支援的 Windows VM。

- 確認 支援的 Linux VM 和核心版本。

- 檢查支援的 計算 儲存體 網路 設定。
SQL 支援 如果您想要移動 SQL 資源,請檢閱 SQL 需求清單
目的地訂用帳戶 目的地區域中的訂用帳戶需要足夠的配額,才能建立您要在目的地區域中移動的資源。 如果沒有配額,請 要求額外的限制
目的地區域費用 確認與您要移動 VM 的目的地區域相關聯的定價和費用。 使用定價計算機 來協助您。

檢閱 PowerShell 需求

大部分的移動資源作業都與使用 Azure 入口網站 或 PowerShell 相同,但有幾個例外狀況。

運算 入口網站 PowerShell
建立移動集合 會自動建立移動集合(您要移動的所有資源清單)。 入口網站會在後端指派必要的身分識別許可權。 您可以使用 PowerShell Cmdlet 來:

- 建立移動集合的資源群組,並為其指定位置。

- 將受控識別指派給集合。

- 將資源新增至集合。
移除移動集合 您無法在入口網站中直接移除移動集合。 您可以使用 PowerShell Cmdlet 來移除移動集合。
資源移動作業

(準備、起始移動、認可等)。
使用 Resource Mover 自動驗證的單一步驟。 PowerShell Cmdlet 至:

1) 驗證相依性。

2) 執行移動。
刪除來源資源 直接在 Resource Mover 入口網站中。 資源類型層級的 PowerShell Cmdlet。

範例值

我們在腳本範例中使用這些值:

設定
訂用帳戶識別碼 subscription-id
來源區域 美國中部
目標區域 美國中西部
資源群組(持有移動集合的中繼資料) RG-MoveCollection-demoRMS
移動集合名稱 PS-centralus-westcentralus-demoRMS
資源群組(來源區域) PSDemoRM
資源群組(目的地區域) PSDemoRM-target
資源移動服務位置 美國東部 2
IdentityType SystemAssigned
要移動的 VM PSDemoVM

登入 Azure

如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶。 然後登入 Azure 入口網站

使用 連線-AzAccount Cmdlet 登入您的 Azure 訂用帳戶:

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

設定移動集合

MoveCollection 物件會儲存您要移動之資源的中繼資料和組態資訊。 若要設定移動集合,請執行下列動作:

  • 建立移動集合的資源群組。
  • 向訂用帳戶註冊服務提供者,以便建立 MoveCollection 資源。
  • 使用受控識別建立 MoveCollection 物件。 若要讓 MoveCollection 物件存取 Resource Mover 服務所在的訂用帳戶,它需要 訂用帳戶信任的系統指派受控識別 (先前稱為受控服務識別 (MSI)。
  • 為受控識別授與 Resource Mover 訂用帳戶的存取權。

註冊資源提供者

  1. 註冊資源提供者 Microsoft.Migrate,以便建立 MoveCollection 資源,如下所示:

    Register-AzResourceProvider -ProviderNamespace Microsoft.Migrate
    
  2. 等候註冊:

    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 物件

建立 MoveCollection 物件,並將受控識別指派給它,如下所示:

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

輸出:

Screenshot of the output text after creating move collection.

授與受控識別的存取權

將受控識別存取權授與 Resource Mover 訂用帳戶,如下所示。 您必須是訂用帳戶擁有者。

  1. 從 MoveCollection 物件擷取識別詳細資料。

    $moveCollection = Get-AzResourceMoverMoveCollection -SubscriptionId $subscriptionId -ResourceGroupName "RG-MoveCollection-demoRMS" -Name "PS-centralus-westcentralus-demoRMS"
    
    $identityPrincipalId = $moveCollection.IdentityPrincipalId
    
  2. 將必要的角色指派給身分識別,讓 Azure Resource Mover 可以存取您的訂用帳戶,以協助移動資源。

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

將資源新增至移動集合

擷取您想要移動之現有來源資源的識別碼。 建立目的地資源設定物件,然後將資源新增至移動集合。

注意

新增至移動集合的資源必須位於相同的訂用帳戶中,但可以位於不同的資源群組中。

新增資源,如下所示:

  1. 取得來源資源識別碼:

    Get-AzResource -Name PSDemoVM -ResourceGroupName PSDemoRM
    

    輸出

    Screenshot of the output text after retrieving the resource ID.

  2. 根據您要移動的資源,建立目標資源設定物件。 在我們的案例中,它是 VM。

    $targetResourceSettingsObj = New-Object Microsoft.Azure.PowerShell.Cmdlets.ResourceMover.Models.Api202101.VirtualMachineResourceSettings
    
  3. 設定物件的資源類型和目標資源名稱。

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

    注意

    我們的目標 VM 名稱與來源區域中的 VM 相同。 您可以選擇不同的名稱。

  4. 使用您擷取/建立的資源識別碼和目標設定物件,將來源資源新增至移動集合。

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

    輸出

    Screenshot of the output text after adding the resource.

驗證和新增相依性

檢查您新增的資源是否具有其他資源的任何相依性,並視需要新增。

  1. 驗證相依性,如下所示:

    Resolve-AzResourceMoverMoveCollectionDependency -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-centralus-westcentralus-demoRMS"
    

    輸出(相依性存在時)

    Screenshot of the output text after validating dependencies.

  2. 身分識別遺漏相依性:

    • 若要擷取所有遺漏相依性的清單:

      Get-AzResourceMoverUnresolvedDependency -MoveCollectionName "PS-centralus-westcentralus-demoRMS" -ResourceGroupName "RG-MoveCollection-demoRMS" -DependencyLevel Descendant
      

      輸出

      Screenshot of the output text after retrieving a list of all dependencies.

    • 只擷取第一層相依性(資源的直接相依性):

      Get-AzResourceMoverUnresolvedDependency -MoveCollectionName "PS-centralus-westcentralus-demoRMS" -ResourceGroupName "RG-MoveCollection-demoRMS" -DependencyLevel Direct
      

      輸出

      Screenshot of the output text after retrieving a list of first-level dependencies.

  3. 若要新增任何未處理的遺漏相依性,請重複上述指示,將 資源新增至移動集合 ,並重新驗證,直到沒有未完成的資源為止。

注意

如果您想要從資源集合中移除資源,請遵循本文 中的 指示。

準備資源

在移動之前,您通常需要在來源區域中準備資源。 例如:

  • 若要移動無狀態資源,例如 Azure 虛擬網路、網路介面卡、負載平衡器和網路安全性群組,您可能需要匯出 Azure Resource Manager 範本。
  • 若要移動 Azure VM 和 SQL 資料庫等具狀態資源,您可能需要開始將資源從來源複寫到目的地區域。

在本教學課程中,由於我們要移動 VM,因此我們必須準備來源資源群組,然後起始並認可其移動,才能開始準備 VM。

注意

如果您有現有的目標資源群組,您可以直接認可來源資源群組的移動,並略過準備和起始移動階段。

準備 VM 資源

準備和移動來源資源群組之後,我們可以準備 VM 資源以進行移動。

  1. 準備 VM 資源之前,請先驗證相依性。

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

    輸出

    Screenshot of the output text after validating the VM before preparing it.

  2. 取得需要與 VM 一起準備的相依資源。

    $resp.AdditionalInfo[0].InfoMoveResource
    

    輸出

    Screenshot of the output text after retrieving dependent VM resources.

  3. 起始所有相依資源的準備程式。

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

    輸出

    Screenshot of the output text after initiating prepare of all resources.

    注意

    您可以提供來源資源識別碼,而不是作為 Prepare Cmdlet 的輸入參數,以及起始移動和認可 Cmdlet 中的資源名稱。 若要這樣做,請執行:

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

起始 VM 資源的移動

  1. 確認 VM 資源處於 起始移動擱置 狀態:

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

    輸出

    Screenshot of the output text after checking initiate state.

  2. 起始移動:

    Invoke-AzResourceMoverInitiateMove -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-centralus-westcentralus-demoRMS"  -MoveResource $('psdemovm111', 'PSDemoRM-vnet','PSDemoVM-nsg', 'PSDemoVM') -MoveResourceInputType "MoveResourceId"
    

    輸出

    Screenshot of the output text after initiating the move of resources.

捨棄或認可?

初始移動之後,您可以決定是否要認可移動或捨棄移動。

  • 捨棄 :如果您要測試,而且不想實際移動來源資源,您可能會捨棄移動。 捨棄移動會將資源傳回起始移動擱置 的狀態 。 然後,您可以視需要重新起始移動。
  • 認可:認可 會完成移至目的地區域的動作。 認可之後,來源資源會處於 [刪除來源擱置 ] 的狀態 ,而您可以決定是否要刪除它。

捨棄移動

若要捨棄移動:

Invoke-AzResourceMoverDiscard -ResourceGroupName "RG-MoveCollection-demoRMS" -MoveCollectionName "PS-centralus-westcentralus-demoRMS"  -MoveResource $('psdemovm111', 'PSDemoRM-vnet','PSDemoVM-nsg', 'PSDemoVM') -MoveResourceInputType "MoveResourceId"

輸出

Screenshot of the output text after discarding the move.

認可移動

  1. 認可移動,如下所示:

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

    輸出

    Screenshot of the output text after committing the move.

  2. 確認所有資源都已移至目的地區域:

    Get-AzResourceMoverMoveResource  -ResourceGroupName "RG-MoveCollection-demoRMS " -MoveCollectionName "PS-centralus-westcentralus-demoRMS"
    

    所有資源現在都處於目的地區域中的 刪除來源擱置 狀態。

刪除來源資源

認可移動,並確認資源在目的地區域中如預期般運作之後,您可以使用 PowerShell 或使用 Azure CLI ,刪除Azure 入口網站 中的每個 來源資源。

下一步

深入瞭解 如何在入口網站中移動 Azure VM。