共用方式為


使用 PowerShell 更新 Azure Cosmos DB 帳戶的區域

適用於:NoSQL MongoDB Cassandra Gremlin Table

此 PowerShell 指令碼會更新 Azure Cosmos DB 帳戶所使用的 Azure 區域。 您可以使用此指令碼來新增 Azure 區域或變更區域容錯移轉順序。

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 若要開始使用,請參閱 安裝 Azure PowerShell。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

必要條件

  • 您需要 Azure 資源群組中現有的 Azure Cosmos DB 帳戶。

  • 此指令碼需要 Azure PowerShell Az 5.4.0 或更新版本。 執行 Get-Module -ListAvailable Az 以列出已安裝的版本。 如果您需要安裝 PowerShell,請參閱安裝 Azure PowerShell 模組

  • 執行 Connect-AzAccount 來登入 Azure。

範例指令碼

Update-AzCosmosDBAccountRegion 命令會更新 Azure Cosmos DB 帳戶的 Azure 區域。 此命令需要資源群組名稱、Azure Cosmos DB 帳戶名稱,以及以所需容錯移轉排序的 Azure 區域清單。

在此指令碼中,Get-AzCosmosDBAccount 命令會取得您指定的 Azure Cosmos DB 帳戶。 New-AzCosmosDBLocationObject 會建立 PSLocation 類型的物件。 Update-AzCosmosDBAccountRegion 會使用 PSLocation 參數來更新帳戶區域。

  • 如果您新增區域,請勿變更相同作業中的第一個容錯移轉區域。 變更個別作業中的容錯移轉優先順序。
  • 您無法如同變更其他 Azure Cosmos DB 帳戶屬性一樣來修改相同作業中的區域。 請個別執行這些作業。

此範例會使用適用於 NoSQL 帳戶的 API。 若要針對其他 API 使用此範例,請複製相關的屬性,並將屬性套用至您的 API 專屬指令碼。

# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Update Cosmos DB account: Add an Azure region (location)
# NOTE: if adding a region to a single master account, do not change the first 
# region in the same operation. Change failover priority order in a
# separate operation.
# NOTE: this operation will return, but account updates may still be
# occurring. Check the account or Resource Group's activity log for status.
# --------------------------------------------------
# Variables - ***** SUBSTITUTE YOUR VALUES *****
$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "myaccount" # Must be all lower case

# Regions ordered by failover priority, with each indicating whether AZ-enabled
# Region AZ status can be checked at https://azure.microsoft.com/global-infrastructure/regions/
$locations = @(
    @{name = "East US"; azEnabled = $true};
    @{name = "West US"; azEnabled = $false};
)
# --------------------------------------------------

Write-Host "Get Cosmos DB account"
$account = Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName

$i = 0
$locationObjects = @()

ForEach ($location in $locations) {
    $locationObjects += New-AzCosmosDBLocationObject -LocationName $location.name -IsZoneRedundant $location.azEnabled -FailoverPriority ($i++)
}

Write-Host "Update Cosmos DB account region(s)"
Update-AzCosmosDBAccountRegion -InputObject $account -LocationObject $locationObjects

雖然指令碼會傳回結果,但更新作業可能無法完成。 使用 Azure Cosmos DB 帳戶 [活動記錄] 來檢查 Azure 入口網站中作業的狀態。

刪除 Azure 資源群組

如果您想要刪除 Azure Cosmos DB 帳戶,可以使用 Remove-AzResourceGroup PowerShell 命令來移除其資源群組。 此命令會移除 Azure 資源群組及其中的所有資源,包括 Azure Cosmos DB 帳戶及其容器和資料庫。

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

下一步