使用 PowerShell 更新 Azure Cosmos DB 帐户上的区域Update the regions on an Azure Cosmos DB account using PowerShell

适用于: SQL API Cassandra API Gremlin API 表 API Azure Cosmos DB API for MongoDB

备注

本文已经过更新,以便使用 Azure Az PowerShell 模块。This article has been updated to use the Azure Az PowerShell module. 若要与 Azure 交互,建议使用的 PowerShell 模块是 Az PowerShell 模块。The Az PowerShell module is the recommended PowerShell module for interacting with Azure. 若要开始使用 Az PowerShell 模块,请参阅安装 Azure PowerShellTo get started with the Az PowerShell module, see Install Azure PowerShell. 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 AzTo learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

本示例要求使用 Azure PowerShell Az 5.4.0 或更高版本。This sample requires Azure PowerShell Az 5.4.0 or later. 运行 Get-Module -ListAvailable Az,查看已安装哪些版本。Run Get-Module -ListAvailable Az to see which versions are installed. 如果需要安装,请参阅安装 Azure PowerShell 模块If you need to install, see Install Azure PowerShell module.

运行 Connect-AzAccount -Environment AzureChinaCloud 以登录到 Azure。Run Connect-AzAccount -Environment AzureChinaCloud to sign in to Azure.

示例脚本Sample script

备注

不能在同一操作中修改区域并更改其他 Cosmos 帐户属性。You cannot modify regions and change other Cosmos account properties in the same operation. 这些操作必须以两个单独的操作来完成。These must be done as two separate operations.

备注

此示例演示如何使用 SQL API 帐户。This sample demonstrates using a SQL API account. 若要将此示例用于其他 API,请复制相关属性,并将其应用于 API 特定的脚本。To use this sample for other APIs, copy the related properties and apply to your API-specific script.

# Reference: Az.CosmosDB | https://docs.microsoft.com/powershell/module/az.cosmosdb
# --------------------------------------------------
# Purpose
# Update Cosmos DB account: Change default consistency level
# --------------------------------------------------
# Variables - ***** SUBSTITUTE YOUR VALUES *****
$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "myaccount" # Must be all lower case
$consistencyLevel = "BoundedStaleness"
$maxStalenessInterval = 300
$maxStalenessPrefix = 100000
# --------------------------------------------------

# Get existing Cosmos DB account
$account = Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName

Write-Host "Updating account $accountName"
Update-AzCosmosDBAccount -InputObject $account `
    -DefaultConsistencyLevel $consistencyLevel `
    -MaxStalenessIntervalInSeconds $maxStalenessInterval `
    -MaxStalenessPrefix $maxStalenessPrefix

清理部署Clean up deployment

运行脚本示例后,可以使用以下命令删除资源组以及与其关联的所有资源。After the script sample has been run, the following command can be used to remove the resource group and all resources associated with it.

Remove-AzResourceGroup -ResourceGroupName "myResourceGroup"

脚本说明Script explanation

此脚本使用以下命令。This script uses the following commands. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 说明Notes
Azure Cosmos DBAzure Cosmos DB
Get-AzCosmosDBAccountGet-AzCosmosDBAccount 列出 Cosmos DB 帐户或获取指定的 Cosmos DB 帐户。Lists Cosmos DB Accounts, or gets a specified Cosmos DB Account.
Update-AzCosmosDBAccountUpdate-AzCosmosDBAccount 更新 Cosmos DB 帐户。Update a Cosmos DB Account.
Azure 资源组Azure Resource Groups
Remove-AzResourceGroupRemove-AzResourceGroup 删除资源组,包括所有嵌套的资源。Deletes a resource group including all nested resources.

后续步骤Next steps

有关 Azure PowerShell 的详细信息,请参阅 Azure PowerShell 文档For more information on the Azure PowerShell, see Azure PowerShell documentation.