將資源移到新的資源群組或訂用帳戶Move resources to a new resource group or subscription

本文示範如何將 Azure 資源移至其他 Azure 訂用帳戶或相同訂用帳戶內的其他資源群組。This article shows you how to move Azure resources to either another Azure subscription or another resource group under the same subscription. 您可以使用 Azure 入口網站、Azure PowerShell、Azure CLI 或 REST API 來移動資源。You can use the Azure portal, Azure PowerShell, Azure CLI, or the REST API to move resources.

移動作業期間會同時鎖定來源群組和目標群組。Both the source group and the target group are locked during the move operation. 資源群組上的寫入和刪除作業將會封鎖,直到移動完成。Write and delete operations are blocked on the resource groups until the move completes. 此鎖定表示您無法新增、更新或刪除資源群組中的資源。This lock means you can't add, update, or delete resources in the resource groups. 這並不表示資源已凍結。It doesn't mean the resources are frozen. 例如,如果您將 SQL Server 和其資料庫移至新的資源群組,使用該資料庫的應用程式不會發生停機時間。For example, if you move a SQL Server and its database to a new resource group, an application that uses the database experiences no downtime. 它仍可對資料庫讀取和寫入。It can still read and write to the database. 鎖定最多可以有四個小時,但大部分的移動會在較少的時間內完成。The lock can last for a maximum of four hours, but most moves complete in much less time.

移動資源只會將它移到新的資源群組或訂用帳戶。Moving a resource only moves it to a new resource group or subscription. 不會變更資源的位置。It doesn't change the location of the resource.

移動資源前的檢查清單Checklist before moving resources

在移動資源之前,有幾個重要步驟需要進行。There are some important steps to do before moving a resource. 藉由驗證這些條件,您可以避免錯誤。By verifying these conditions, you can avoid errors.

  1. 您想要移動的資源必須支援移動作業。The resources you want to move must support the move operation. 如需哪些資源支援移動的清單,請參閱資源的移動作業支援For a list of which resources support move, see Move operation support for resources.

  2. 某些服務在移動資源時有特定的限制或需求。Some services have specific limitations or requirements when moving resources. 如果您已移動下列任何服務,請在移動前檢查該指引。If you've moving any of the following services, check that guidance before moving.

  3. 來源和目的地訂用帳戶必須為作用中。The source and destination subscriptions must be active. 如果您在啟用已停用的帳戶時遇到問題,請建立 Azure 支援要求If you have trouble enabling an account that has been disabled, create an Azure support request. 針對問題類型選取 [訂用帳戶管理]。Select Subscription Management for the issue type.

  4. 來源和目的地的訂用帳戶必須存在於相同的 Azure Active Directory 租用戶內。The source and destination subscriptions must exist within the same Azure Active Directory tenant. 若要檢查這兩個訂用帳戶都有相同的租用戶識別碼,請使用 Azure PowerShell 或 Azure CLI。To check that both subscriptions have the same tenant ID, use Azure PowerShell or Azure CLI.

    如果是 Azure PowerShell,請使用:For Azure PowerShell, use:

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

    對於 Azure CLI,請使用:For Azure CLI, use:

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

    如果來源和目的地訂用帳戶的租用戶識別碼不相同,請使用下列方法來協調租用戶識別碼:If the tenant IDs for the source and destination subscriptions aren't the same, use the following methods to reconcile the tenant IDs:

  5. 必須針對要移動之資源的資源提供者註冊其目的地訂用帳戶。The destination subscription must be registered for the resource provider of the resource being moved. 否則,您會收到錯誤,指出 未針對資源類型註冊訂用帳戶If not, you receive an error stating that the subscription is not registered for a resource type. 將資源移至新的訂用帳戶時,可能會因為不曾以指定的資源類型使用過該訂用帳戶,因而出現此錯誤。You might see this error when moving a resource to a new subscription, but that subscription has never been used with that resource type.

    針對 PowerShell,使用下列命令來取得註冊狀態:For PowerShell, use the following commands to get the registration status:

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

    若要註冊資源提供者,請使用:To register a resource provider, use:

    Register-AzResourceProvider -ProviderNamespace Microsoft.Batch

    針對 Azure CLI,使用下列命令來取得註冊狀態:For Azure CLI, use the following commands to get the registration status:

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

    若要註冊資源提供者,請使用:To register a resource provider, use:

    az provider register --namespace Microsoft.Batch
  6. 移動資源的帳戶至少必須有下列權限:The account moving the resources must have at least the following permissions:

    • 來源資源群組上的 Microsoft.Resources/subscriptions/resourceGroups/moveResources/actionMicrosoft.Resources/subscriptions/resourceGroups/moveResources/action on the source resource group.
    • 來源資源群組上的 Microsoft.Resources/subscriptions/resourceGroups/writeMicrosoft.Resources/subscriptions/resourceGroups/write on the destination resource group.
  7. 移動資源之前,請針對您要將資源移入的訂用帳戶,檢查其訂用帳戶配額。Before moving the resources, check the subscription quotas for the subscription you're moving the resources to. 如果移動資源表示訂用帳戶會超出限制,那麼您必須檢閱您是否可以要求增加配額。If moving the resources means the subscription will exceed its limits, you need to review whether you can request an increase in the quota. 如需限制清單和如何要求增加配額的資訊,請參閱 Azure 訂用帳戶和服務限制、配額及條件約束For a list of limits and how to request an increase, see Azure subscription and service limits, quotas, and constraints.

  8. 針對跨訂用帳戶移動,資源和其相依資源必須位於相同的資源群組中,而且必須一起移動。For a move across subscriptions, the resource and its dependent resources must be located in the same resource group and they must be moved together. 例如,具有受控磁片的 VM 需要將 VM 和受控磁片一起移動,以及其他相依資源。For example, a VM with managed disks would require the VM and the managed disks to be moved together, along with other dependent resources.

    如果您要將資源移到新的訂用帳戶,請查看資源是否有任何相依資源,以及它們是否位於相同的資源群組中。If you're moving a resource to a new subscription, check to see whether the resource has any dependent resources, and whether they're located in the same resource group. 如果資源不在相同的資源群組中,請檢查資源是否可以合併到相同的資源群組中。If the resources aren't in the same resource group, check to see whether the resources can be consolidated into the same resource group. 若是如此,請在資源群組之間使用移動作業,將所有這些資源帶入相同的資源群組。If so, bring all these resources into the same resource group by using a move operation across resource groups.

    如需詳細資訊,請參閱跨訂用帳戶移動的案例For more information, see Scenario for move across subscriptions.

跨訂用帳戶移動的案例Scenario for move across subscriptions

將資源從一個訂用帳戶移至另一個訂用帳戶是三個步驟的程式:Moving resources from one subscription to another is a three-step process:


為了方便說明,我們只有一個相依資源。For illustration purposes, we have only one dependent resource.

  • 步驟1:如果相依資源分散到不同的資源群組,請先將它們移到一個資源群組中。Step 1: If dependent resources are distributed across different resource groups, first move them into one resource group.
  • 步驟2:將資源和相依資源與來源訂用帳戶一起移至目標訂用帳戶。Step 2: Move the resource and dependent resources together from the source subscription to the target subscription.
  • 步驟3:選擇性地將相依資源重新散發至目標訂用帳戶中的不同資源群組。Step 3: Optionally, redistribute the dependent resources to different resource groups within the target subscription.

驗證移動Validate move

驗證移動作業可讓您直接測試移動案例,而不需要實際移動資源。The validate move operation lets you test your move scenario without actually moving the resources. 使用此作業來檢查移動是否成功。Use this operation to check if the move will succeed. 當您傳送移動要求時,會自動呼叫驗證。Validation is automatically called when you send a move request. 只有當您需要預先確定結果時,才使用此作業。Use this operation only when you need to predetermine the results. 若要執行這項作業,您需要:To run this operation, you need the:

  • 來源資源群組的名稱name of the source resource group
  • 目標資源群組的資源識別碼resource ID of the target resource group
  • 要移動的每個資源所具備的資源識別碼resource ID of each resource to move
  • 帳戶的存取權杖the access token for your account

傳送下列要求:Send the following request:

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

使用要求本文:With a request body:

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

如果要求的格式正確,此作業將會傳回:If the request is formatted correctly, the operation returns:

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 狀態碼指出已接受驗證要求,但尚未判斷移動作業是否會成功。The 202 status code indicates the validation request was accepted, but it hasn't yet determined if the move operation will succeed. location 值包含一個 URL,可讓您用來檢查長時間執行作業的狀態。The location value contains a URL that you use to check the status of the long-running operation.

若要檢查狀態,請傳送下列要求:To check the status, send the following request:

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

當作業仍在執行時,您會持續收到 202 狀態碼。While the operation is still running, you continue to receive the 202 status code. 請等候 retry-after 值中指出的秒數,再重新嘗試。Wait the number of seconds indicated in the retry-after value before trying again. 如果移動作業驗證成功,您會收到 204 狀態碼。If the move operation validates successfully, you receive the 204 status code. 如果移動驗證失敗,則會收到錯誤訊息,例如:If the move validation fails, you receive an error message, such as:


使用入口網站Use the portal

若要移動資源,請選取具有這些資源的資源群組,然後選取 [移動] 按鈕。To move resources, select the resource group with those resources, and then select the Move button.


選擇將資源移動到新的資源群組或新的訂用帳戶。Select whether you're moving the resources to a new resource group or a new subscription.

選取要移動的資源和目的地資源群組。Select the resources to move and the destination resource group. 認可您需要更新這些資源的指令碼,然後選取 [確定]。Acknowledge that you need to update scripts for these resources and select OK. 如果您在上一個步驟中選取了 [編輯訂用帳戶] 圖示,則也必須選取目的地訂用帳戶。If you selected the edit subscription icon in the previous step, you must also select the destination subscription.


在 [通知]中,您會看到移動作業正在執行。In Notifications, you see that the move operation is running.


完成後,您會收到結果的通知。When it has completed, you're notified of the result.


如果您收到錯誤,請參閱針對將 Azure 資源移至新的資源群組或訂用帳戶進行疑難排解If you get an error, see Troubleshoot moving Azure resources to new resource group or subscription.

使用 Azure PowerShellUse Azure PowerShell

若要將現有的資源移動到另一個資源群組或訂用帳戶,請使用 Move-AzResource 命令。To move existing resources to another resource group or subscription, use the Move-AzResource command. 下列範例示範了如何將多個資源移動到新的資源群組。The following example shows how to move several resources to a new resource group.

$webapp = Get-AzResource -ResourceGroupName OldRG -ResourceName ExampleSite
$plan = Get-AzResource -ResourceGroupName OldRG -ResourceName ExamplePlan
Move-AzResource -DestinationResourceGroupName NewRG -ResourceId $webapp.ResourceId, $plan.ResourceId

若要移動到新的訂用帳戶,請包含 DestinationSubscriptionId參數的值。To move to a new subscription, include a value for the DestinationSubscriptionId parameter.

如果您收到錯誤,請參閱針對將 Azure 資源移至新的資源群組或訂用帳戶進行疑難排解If you get an error, see Troubleshoot moving Azure resources to new resource group or subscription.

使用 Azure CLIUse Azure CLI

若要將現有的資源移動到另一個資源群組或訂用帳戶,請使用 az resource move 命令。To move existing resources to another resource group or subscription, use the az resource move command. 提供要移動之資源的資源識別碼。Provide the resource IDs of the resources to move. 下列範例示範了如何將多個資源移動到新的資源群組。The following example shows how to move several resources to a new resource group. 請在 --ids 參數中,為要移動的資源識別碼提供以空格分隔的清單。In the --ids parameter, provide a space-separated list of the resource IDs to move.

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 參數。To move to a new subscription, provide the --destination-subscription-id parameter.

如果您收到錯誤,請參閱針對將 Azure 資源移至新的資源群組或訂用帳戶進行疑難排解If you get an error, see Troubleshoot moving Azure resources to new resource group or subscription.


若要將現有的資源移至另一個資源群組或訂用帳戶,請使用移動資源作業。To move existing resources to another resource group or subscription, use the Move resources operation.

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

在要求主體中,您可以指定目標資源群組以及要移動的資源。In the request body, you specify the target resource group and the resources to move.

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

如果您收到錯誤,請參閱針對將 Azure 資源移至新的資源群組或訂用帳戶進行疑難排解If you get an error, see Troubleshoot moving Azure resources to new resource group or subscription.

常見問題集Frequently asked questions

問題:我的資源移動作業(通常需要幾分鐘的時間)已執行一小時。是否有任何問題?Question: My resource move operation, which usually takes a few minutes, has been running for almost an hour. Is there something wrong?

移動資源是一種複雜的作業,具有不同的階段。Moving a resource is a complex operation that has different phases. 它不只是您想要移動之資源的資源提供者。It can involve more than just the resource provider of the resource you're trying to move. 由於資源提供者之間的相依性,Azure Resource Manager 允許4小時讓作業完成。Because of the dependencies between resource providers, Azure Resource Manager allows 4 hours for the operation to complete. 這段時間可讓資源提供者有機會從暫時性問題中復原。This time period gives resource providers a chance to recover from transient issues. 如果您的移動要求在4小時內,則作業會繼續嘗試完成,而且可能仍會成功。If your move request is within the 4-hour period, the operation keeps trying to complete and may still succeed. 這段期間的來源和目的地資源群組都會鎖定,以避免發生一致性問題。The source and destination resource groups are locked during this time to avoid consistency issues.

問題:在資源移動期間,我的資源群組為何會鎖定4小時?Question: Why is my resource group locked for 4 hours during resource move?

[4 小時] 視窗是資源移動所允許的最長時間。The 4-hour window is the maximum time allowed for resource move. 為了防止修改所移動的資源,來源和目的地資源群組會在資源移動期間遭到鎖定。To prevent modifications on the resources being moved, both the source and destination resource groups are locked for the duration of the resource move.

移動要求中有兩個階段。There are two phases in a move request. 在第一個階段中,會移動資源。In the first phase, the resource is moved. 在第二個階段中,通知會傳送給相依于所移動資源的其他資源提供者。In the second phase, notifications are sent to other resource providers that are dependent on the resource being moved. 當資源提供者失敗任一階段時,可以針對整個4小時時間範圍鎖定資源群組。A resource group can be locked for the entire 4-hour window when a resource provider fails either phase. 在允許的時間內,Resource Manager 重試失敗的步驟。During the allowed time, Resource Manager retries the failed step.

如果資源無法在4小時的時間範圍內移動,Resource Manager 會將這兩個資源群組解除鎖定。If a resource can't be moved within the 4-hour window, Resource Manager unlocks both resource groups. 已成功移動的資源會位於目的地資源群組中。Resources that were successfully moved are in the destination resource group. 無法移動的資源會保留在來源資源群組中。Resources that failed to move are left the source resource group.

問題:資源移動期間鎖定的來源和目的地資源群組有何影響?Question: What are the implications of the source and destination resource groups being locked during the resource move?

此鎖定可防止您刪除任一資源群組、在任一資源群組中建立新的資源,或刪除與移動相關的任何資源。The lock prevents you from deleting either resource group, creating a new resource in either resource group, or deleting any of the resources involved in the move.

當使用者嘗試刪除屬於進行中移動的資源群組時,下圖顯示來自 Azure 入口網站的錯誤訊息。The following image shows an error message from the Azure portal when a user tries to delete a resource group that is part of an ongoing move.


問題:錯誤碼 "MissingMoveDependentResources" 代表什麼意思?Question: What does the error code "MissingMoveDependentResources" mean?

移動資源時,其相依資源必須存在於目的地資源群組或訂用帳戶中,或包含在移動要求中。When moving a resource, its dependent resources must either exist in the destination resource group or subscription, or be included in the move request. 當相依資源不符合此需求時,您會收到 MissingMoveDependentResources 錯誤碼。You get the MissingMoveDependentResources error code when a dependent resource doesn't meet this requirement. 錯誤訊息具有需要包含在移動要求中之相依資源的詳細資料。The error message has details about the dependent resource that needs to be included in the move request.

例如,移動虛擬機器可能需要移動七個資源類型與三個不同的資源提供者。For example, moving a virtual machine could require moving seven resource types with three different resource providers. 這些資源提供者和類型如下:Those resource providers and types are:

  • Microsoft.ComputeMicrosoft.Compute
    • virtualMachinesvirtualMachines
    • disksdisks
  • Microsoft.NetworkMicrosoft.Network
    • networkInterfacesnetworkInterfaces
    • publicIPAddressespublicIPAddresses
    • networkSecurityGroupsnetworkSecurityGroups
    • virtualNetworksvirtualNetworks
  • Microsoft.StorageMicrosoft.Storage
    • storageAccountsstorageAccounts

另一個常見的範例牽涉到移動虛擬網路。Another common example involves moving a virtual network. 您可能必須移動數個與該虛擬網路相關聯的其他資源。You may have to move several other resources associated with that virtual network. 移動要求可能需要移動公用 IP 位址、路由表、虛擬網路閘道、網路安全性群組,以及其他專案。The move request could require moving public IP addresses, route tables, virtual network gateways, network security groups, and others.

問題:為什麼我無法在 Azure 中移動某些資源?Question: Why can’t I move some resources in Azure?

目前,並非中的所有資源都 Azure 支援移動。Currently, not all resources in Azure support move. 如需支援移動的資源清單,請參閱資源的移動作業支援For a list of resources that support move, see Move operation support for resources.

後續步驟Next steps

如需哪些資源支援移動的清單,請參閱資源的移動作業支援For a list of which resources support move, see Move operation support for resources.