將 Azure 資源移至新的資源群組或訂用帳戶 (機器翻譯)

本文示範如何將 Azure 資源移至其他 Azure 訂用帳戶或相同訂用帳戶內的其他資源群組。 您可以使用 Azure 入口網站、Azure PowerShell、Azure CLI 或 REST API 來移動資源。

移動作業期間會同時鎖定來源群組和目標群組。 資源群組上的寫入和刪除作業將會封鎖,直到移動完成。 此鎖定表示您無法新增、更新或刪除資源群組中的資源。 這並不表示資源已凍結。 例如,如果您將 Azure SQL 邏輯伺服器、其資料庫和其他相依資源移至新的資源群組或訂用帳戶,使用這些資料庫的應用程式並不會停止運作。 這些應用程式仍可讀取和寫入資料庫。 鎖定最多可以持續四個小時,但大部分的移動可在較短的時間內完成。

如果您的移動需要設定新的相依資源,這些服務將出現中斷的狀況,直到重新設定為止。

移動資源只會將它移到新的資源群組或訂閱。 不會變更資源的位置。

已變更資源識別碼

當您移動資源時,會變更其資源識別碼。 資源識別碼的標準格式為 /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}。 當您將資源移至新的資源群組或訂用帳戶時,您會變更該路徑中的一或多個值。

如果您在任何位置使用資源識別碼,您需要變更該值。 例如,如果您在入口網站中有參考資源識別碼的自訂儀表板,您必須更新該值。 尋找需要針對新資源識別碼更新的任何指令碼或範本。

移動資源前的檢查清單

在移動資源之前,有幾個重要步驟需要進行。 藉由驗證這些條件,您可以避免錯誤。

  1. 來源和目的地訂用帳戶必須為作用中。 如果您在啟用已停用的帳戶時遇到問題,請建立 Azure 支援要求。 針對問題類型選取 [訂用帳戶管理]

  2. 來源和目的地的訂用帳戶必須存在於相同的 Microsoft Entra 租用戶內。 若要檢查這兩個訂用帳戶都有相同的租用戶識別碼,請使用 Azure PowerShell 或 Azure CLI。

    如果是 Azure PowerShell,請使用:

    (Get-AzSubscription -SubscriptionName <your-source-subscription>).TenantId
    (Get-AzSubscription -SubscriptionName <your-destination-subscription>).TenantId
    

    對於 Azure CLI,請使用:

    az account show --subscription <your-source-subscription> --query tenantId
    az account show --subscription <your-destination-subscription> --query tenantId
    

    如果來源和目的地訂用帳戶的租用戶識別碼不相同,請使用下列方法來協調租用戶識別碼:

  3. 如果您嘗試將資源移入或移出雲端解決方案提供者 (CSP) 合作夥伴,請參閱在訂閱者與 CSP 之間轉移 Azure 訂用帳戶

  4. 您要移動的資源必須支援移動作業。 如需支援移動的資源清單,請參閱資源的移動作業支援

  5. 某些服務在移動資源時有特定的限制或需求。 如果您要移動下列任何服務,請在移動前先查看指引。

  6. 必須針對要移動之資源的資源提供者註冊其目的地訂用帳戶。 否則,您會收到錯誤,指出 未針對資源類型註冊訂用帳戶。 將資源移至新的訂用帳戶時,可能會因為不曾以指定的資源類型使用過該訂用帳戶,因而出現此錯誤。

    針對 PowerShell,使用下列命令來取得註冊狀態:

    Set-AzContext -Subscription <destination-subscription-name-or-id>
    Get-AzResourceProvider -ListAvailable | Select-Object ProviderNamespace, RegistrationState
    

    若要註冊資源提供者,請使用:

    Register-AzResourceProvider -ProviderNamespace Microsoft.Batch
    

    針對 Azure CLI,使用下列命令來取得註冊狀態:

    az account set -s <destination-subscription-name-or-id>
    az provider list --query "[].{Provider:namespace, Status:registrationState}" --out table
    

    若要註冊資源提供者,請使用:

    az provider register --namespace Microsoft.Batch
    
  7. 移動資源之前,請針對您要將資源移入的訂用帳戶,檢查其訂用帳戶配額。 如果移動資源表示訂用帳戶會超出限制,那麼您必須檢閱您是否可以要求增加配額。 如需限制清單和如何要求增加配額的資訊,請參閱 Azure 訂用帳戶和服務限制、配額及條件約束

  8. 移動資源的帳戶至少必須有下列權限:

    • 來源資源群組上的 Microsoft.Resources/subscriptions/resourceGroups/moveResources/action
    • 來源資源群組上的 Microsoft.Resources/subscriptions/resourceGroups/write
  9. 如果您將直接被指派了 Azure 角色的資源移至資源 (或子資源),角色指派並不會移動,而會變成孤立的角色指派。 移動之後,您必須重新建立角色指派。 孤立的角色指派最終會自動移除,但建議您在移動之前先移除角色指派。

    如需如何管理角色指派的詳細資訊,請參閱列出 Azure 角色指派指派 Azure 角色

  10. 若要進行跨訂用帳戶的移動,資源及其相依的資源必須位於相同的資源群組中,且必須一起移動。 例如,具有受控磁碟的 VM 需要將 VM 和受控磁碟連同其他相依資源一起移動。

    如果您要將資源移至新的訂用帳戶,請確認資源是否有任何相依的資源,以及是否位於相同的資源群組中。 如果資源不在相同的資源群組中,請確認資源是否可以合併到相同的資源群組中。 如果可以,請使用跨資源群組的移動作業,將這些資源全部放入相同的資源群組。

    如需詳細資訊,請參閱在訂用帳戶之間移動的案例

在訂用帳戶之間移動的案例

將資源從一個訂用帳戶移至另一個訂用帳戶時,須執行三個步驟:

Diagram that shows the three-step process of moving resources across subscriptions.

為了方便說明,我們僅包含一個相依資源。

  • 步驟 1:如果相依資源分散在不同的資源群組間,請先將其移至一個資源群組中。
  • 步驟 2:將資源和相依資源一起從來源訂用帳戶移至目標訂用帳戶。
  • 步驟 3:選擇性地將相依資源重新發佈至目標訂用帳戶內的不同資源群組。

使用入口網站

若要移動資源,請選取包含這些資源的資源群組。

選取您要移動的資源。 若要移動所有資源,請選取清單最上方的核取方塊。 或者,個別選取資源。

Screenshot of the Azure portal showing the selection of resources to move.

選取 [移動] 按鈕。

Screenshot of the Azure portal displaying the Move button with three options.

此按鈕提供三個選項:

選擇將資源移動到新的資源群組或新的訂用帳戶。

來源資源群組會自動設定。 指定目的地資源群組。 如果您要移至新的訂用帳戶,也請指定訂用帳戶。 選取 [下一步]。

Screenshot of the Azure portal where the user specifies the destination resource group for the move operation.

入口網站會驗證資源可以移動。 等待驗證完成。

Screenshot of the Azure portal showing the validation process for the move operation.

在驗證順利完成後,選取 [下一步]。

確認您需要更新這些資源的工具和指令碼。 若要開始移動資源,請選取 [移動]。

Screenshot of the Azure portal where the user acknowledges the need to update tools and scripts before starting the move operation.

移動完成後,您會收到結果的通知。

Screenshot of the Azure portal displaying a notification with the results of the move operation.

使用 Azure PowerShell

Validate

若要測試您的移動案例而不實際移動資源,請使用 Invoke-AzResourceAction 命令。 只有當您需要預先確定結果時,才使用此命令。

$sourceName = "sourceRG"
$destinationName = "destinationRG"
$resourcesToMove = @("app1", "app2")

$sourceResourceGroup = Get-AzResourceGroup -Name $sourceName
$destinationResourceGroup = Get-AzResourceGroup -Name $destinationName

$resources = Get-AzResource -ResourceGroupName $sourceName | Where-Object { $_.Name -in $resourcesToMove }

Invoke-AzResourceAction -Action validateMoveResources `
   -ResourceId $sourceResourceGroup.ResourceId `
   -Parameters @{
      resources = $resources.ResourceId;  # Wrap in an @() array if providing a single resource ID string.
      targetResourceGroup = $destinationResourceGroup.ResourceId
   }

如果通過驗證,您就不會看到任何輸出。

如果驗證失敗,您會看到錯誤訊息,說明無法移動資源的原因。

移動

若要將現有的資源移動到另一個資源群組或訂用帳戶,請使用 Move-AzResource 命令。 下列範例示範了如何將多個資源移動到新的資源群組。

$sourceName = "sourceRG"
$destinationName = "destinationRG"
$resourcesToMove = @("app1", "app2")

$resources = Get-AzResource -ResourceGroupName $sourceName | Where-Object { $_.Name -in $resourcesToMove }

Move-AzResource -DestinationResourceGroupName $destinationName -ResourceId $resources.ResourceId

若要移動到新的訂用帳戶,請包含 DestinationSubscriptionId參數的值。

使用 Azure CLI

Validate

若要測試您的移動案例而不實際移動資源,請使用 az resource invoke-action 命令。 只有當您需要預先確定結果時,才使用此命令。 若要執行這項作業,您需要:

  • 來源資源群組的資源識別碼
  • 目標資源群組的資源識別碼
  • 要移動的每個資源的資源識別碼

在要求本文中,使用 \" 來逸出雙引號。

az resource invoke-action --action validateMoveResources \
  --ids "/subscriptions/{subscription-id}/resourceGroups/{source-rg}" \
  --request-body "{  \"resources\": [\"/subscriptions/{subscription-id}/resourceGroups/{source-rg}/providers/{resource-provider}/{resource-type}/{resource-name}\", \"/subscriptions/{subscription-id}/resourceGroups/{source-rg}/providers/{resource-provider}/{resource-type}/{resource-name}\", \"/subscriptions/{subscription-id}/resourceGroups/{source-rg}/providers/{resource-provider}/{resource-type}/{resource-name}\"],\"targetResourceGroup\":\"/subscriptions/{subscription-id}/resourceGroups/{destination-rg}\" }" 

如果通過驗證,您會看到:

{} Finished .. 

如果驗證失敗,您會看到錯誤訊息,說明無法移動資源的原因。

移動

若要將現有的資源移動到另一個資源群組或訂用帳戶,請使用 az resource move 命令。 請在 --ids 參數中,為要移動的資源識別碼提供以空格分隔的清單。

下列範例示範了如何將多個資源移動到新的資源群組。 它可在 Bash 終端機中使用 Azure CLI 時運作。

webapp=$(az resource show -g OldRG -n ExampleSite --resource-type "Microsoft.Web/sites" --query id --output tsv)
plan=$(az resource show -g OldRG -n ExamplePlan --resource-type "Microsoft.Web/serverfarms" --query id --output tsv)
az resource move --destination-group newgroup --ids $webapp $plan

下一個範例示範如何在 PowerShell 主控台中執行相同的命令。

$webapp=$(az resource show -g OldRG -n ExampleSite --resource-type "Microsoft.Web/sites" --query id --output tsv)
$plan=$(az resource show -g OldRG -n ExamplePlan --resource-type "Microsoft.Web/serverfarms" --query id --output tsv)
az resource move --destination-group newgroup --ids $webapp $plan

若要移動到新的訂用帳戶,請提供 --destination-subscription-id 參數。

使用 Python

Validate

若要測試您的移動案例,而不實際移動資源,請使用 ResourceManagementClient.resources.begin_validate_move_resources 方法。 只有在您需要預先確定結果時,才使用此方法。

import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient

credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

resource_client = ResourceManagementClient(credential, subscription_id)

source_name = "sourceRG"
destination_name = "destinationRG"
resources_to_move = ["app1", "app2"]

destination_resource_group = resource_client.resource_groups.get(destination_name)

resources = [
    resource for resource in resource_client.resources.list_by_resource_group(source_name)
    if resource.name in resources_to_move
]

resource_ids = [resource.id for resource in resources]

validate_move_resources_result = resource_client.resources.begin_validate_move_resources(
    source_name,
    {
        "resources": resource_ids,
        "target_resource_group": destination_resource_group.id
    }
).result()

print("Validate move resources result: {}".format(validate_move_resources_result))

如果通過驗證,您就不會看到任何輸出。

如果驗證失敗,您會看到錯誤訊息,說明無法移動資源的原因。

移動

若要將現有資源移至另一個資源群組或訂用帳戶,請使用 ResourceManagementClient.resources.begin_move_resources 方法。 下列範例示範了如何將多個資源移動到新的資源群組。

import os
from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient

credential = AzureCliCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

resource_client = ResourceManagementClient(credential, subscription_id)

source_name = "sourceRG"
destination_name = "destinationRG"
resources_to_move = ["app1", "app2"]

destination_resource_group = resource_client.resource_groups.get(destination_name)

resources = [
    resource for resource in resource_client.resources.list_by_resource_group(source_name)
    if resource.name in resources_to_move
]

resource_ids = [resource.id for resource in resources]

resource_client.resources.begin_move_resources(
    source_name,
    {
        "resources": resource_ids,
        "target_resource_group": destination_resource_group.id
    }
)

使用 REST API

Validate

驗證移動作業可讓您直接測試移動案例,而不需要實際移動資源。 使用此作業以檢查移動是否成功。 當您傳送移動要求時,會自動呼叫驗證。 只有當您需要預先確定結果時,才使用此作業。 若要執行這項作業,您需要:

  • 來源資源群組的名稱
  • 目標資源群組的資源識別碼
  • 要移動的每個資源的資源識別碼
  • 帳戶的存取權杖

傳送下列要求:

POST https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<source-group>/validateMoveResources?api-version=2019-05-10
Authorization: Bearer <access-token>
Content-type: application/json

使用要求本文:

{
 "resources": ["<resource-id-1>", "<resource-id-2>"],
 "targetResourceGroup": "/subscriptions/<subscription-id>/resourceGroups/<target-group>"
}

如果要求的格式正確,此作業將會傳回:

Response Code: 202
cache-control: no-cache
pragma: no-cache
expires: -1
location: https://management.azure.com/subscriptions/<subscription-id>/operationresults/<operation-id>?api-version=2018-02-01
retry-after: 15
...

202 狀態碼指出已接受驗證要求,但尚未判斷移動作業是否會成功。 location 值包含一個 URL,可讓您用來檢查長時間執行作業的狀態。

若要檢查狀態,請傳送下列要求:

GET <location-url>
Authorization: Bearer <access-token>

當作業仍在執行時,您會持續收到 202 狀態碼。 請等候 retry-after 值中指出的秒數,再重新嘗試。 如果移動作業驗證成功,您會收到 204 狀態碼。 如果移動驗證失敗,則會收到錯誤訊息,例如:

{"error":{"code":"ResourceMoveProviderValidationFailed","message":"<message>"...}}

移動

若要將現有的資源移動到另一個資源群組或訂用帳戶,請使用移動資源作業。

POST https://management.azure.com/subscriptions/{source-subscription-id}/resourcegroups/{source-resource-group-name}/moveResources?api-version={api-version}

在要求主體中,您可以指定目標資源群組以及要移動的資源。

{
 "resources": ["<resource-id-1>", "<resource-id-2>"],
 "targetResourceGroup": "/subscriptions/<subscription-id>/resourceGroups/<target-group>"
}

常見問題集

問題:通常只需要幾分鐘的資源移動作業,已經執行了幾乎一個小時。 是不是哪裡出錯了?

移動資源是具有不同階段的複雜作業。 牽涉到的可能不只是您嘗試移動資源的資源提供者。 由於資源提供者之間的相依性,Azure Resource Manager 有 4 小時的時間可讓作業完成。 這段時間可讓資源提供者有機會從暫時性問題中復原。 如果您的移動要求是在四小時內,則作業會繼續嘗試完成,且可能仍會成功。 在這段時間會鎖定來源和目的地資源群組,以避免發生一致性問題。

問題:為什麼我的資源群組在資源移動期間鎖定了四個小時?

移動要求完成最多需要四個小時。 為了防止對移動的資源進行修改,來源和目的地資源群組在資源移動期間都會鎖定。

移動要求中有兩個階段。 在第一個階段中,會移動資源。 在第二個階段中,會將通知傳送給相依於要移動資源的其他資源提供者。 當資源提供者在任一階段失敗時,資源群組會鎖定整整四個小時。 在允許的時間內,Resource Manager 會重試失敗的步驟。

如果無法在四個小時內移動資源,Resource Manager 會將這兩個資源群組解除鎖定。 成功移動的資源會位於目的地資源群組中。 移動失敗的資源會留在來源資源群組中。

問題:來源和目的地資源群組在資源移動期間鎖定有什麼含意?

鎖定可以防止您刪除其中一個資源群組、在任何一個資源群組中建立新的資源,或刪除移動涉及的任何資源。

下圖顯示當使用者嘗試刪除屬於進行中移動的資源群組時,來自 Azure 入口網站的錯誤訊息。

Screenshot of the Azure portal showing an error message when trying to delete a resource group involved in an ongoing move operation.

問題:錯誤碼 "MissingMoveDependentResources" 是什麼意思?

當您移動資源時,其相依資源必須存在於目的地資源群組或訂用帳戶中,或包含在移動要求中。 當相依資源不符合這項需求時,您會收到 MissingMoveDependentResources 錯誤碼。 此錯誤訊息具有需要包含在移動要求中相依資源的詳細資料。

例如,移動虛擬機器可能需要移動三個不同資源提供者的七個資源類型。 這些資源提供者和類型為:

  • Microsoft.Compute

    • virtualMachines
    • disks
  • Microsoft.Network

    • networkInterfaces
    • publicIPAddresses
    • networkSecurityGroups
    • virtualNetworks
  • Microsoft.Storage

    • storageAccounts

另一個常見的範例牽涉到移動虛擬網路。 您可能必須移動多個與該虛擬網路相關聯的其他資源。 移動要求可能需要移動公用 IP 位址、路由表、虛擬網路閘道、網路安全性群組等等。 一般而言,虛擬網路閘道必須一律位於與其虛擬網路相同的資源群組中,因此無法單獨移動。

問題:錯誤碼 "RequestDisallowedByPolicy" 是什麼意思?

Resource Manager 在嘗試移動之前驗證您的移動要求。 此驗證包括檢查與移動相關資源上定義的原則。 例如,如果您嘗試移動金鑰保存庫,但是您的組織具有拒絕在目標資源群組中建立金鑰保存庫的原則,則驗證會失敗並封鎖移動。 傳回的錯誤碼為 RequestDisallowedByPolicy

如需原則的詳細資訊,請參閱什麼是 Azure 原則?

問題:為什麼我無法移動 Azure 中的某些資源?

目前並非 Azure 中的所有資源都支援移動。 如需支援移動的資源清單,請參閱資源的移動作業支援

問題:我在單一作業中可以移動多少資源?

可能的話,請將大型移動細分為個別的移動作業。 單一作業超過 800 個資源時,Resource Manager 會立即傳回錯誤。 不過,移動少於 800 個資源也可能因為逾時而失敗。

問題:資源未處於成功狀態的錯誤代表什麼意義?

當您收到錯誤訊息,指出無法移動某個資源,因為它不是處於成功狀態,它實際上可能是封鎖移動的相依資源。 錯誤碼通常是 MoveCannotProceedWithResourcesNotInSucceededState

如果來源或目標資源群組包含虛擬網路,則會在移動期間檢查虛擬網路所有相依資源的狀態。 該檢查包含直接與間接相依於虛擬網路的資源。 如果有任何資源處於失敗狀態,則會封鎖移動。 例如,如果使用虛擬網路的虛擬機器失敗,則會封鎖移動。 即使虛擬機器不是正在移動的其中一個資源,且不在移動的其中一個資源群組中,該移動也遭到封鎖。

當您收到此錯誤時,您有兩個選項。 請將您的資源移至沒有虛擬網路的資源群組,或 聯絡支援人員

下一步

如需支援移動的資源清單,請參閱資源的移動作業支援