Share via


使用 PowerShell 來管理 Azure Cosmos DB for NoSQL 資源

適用於:NoSQL

下列指南說明如何使用 PowerShell 來為 Azure Cosmos DB for NoSQL 資源 (包括 Azure Cosmos DB 帳戶、資料庫、容器和輸送量) 編寫指令碼並將其管理工作自動化。 如需適用於其他 API 的 PowerShell Cmdlet,請參閱適用於 Cassandra 的 PowerShell 範例適用於 API for MongoDB 的 PowerShell 範例適用於 Gremlin 的 PowerShell 範例適用於 Table 的 PowerShell 範例

注意

本文中的範例使用 Az.CosmosDB 管理 Cmdlet。 如需有關最新變更的相關資訊,請參閱 Az.CosmosDB API 參考頁面。

針對 Azure Cosmos DB 的跨平台管理,您可以使用 AzAz.CosmosDB Cmdlet 來搭配 跨平台 PowerShell,也可使用 Azure CLIREST APIAzure 入口網站

注意

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

快速入門

請遵循如何安裝和設定 Azure PowerShell 的指示進行安裝,並在 PowerShell 中登入 Azure 帳戶。

重要

無法重新命名 Azure Cosmos DB 資源,因為這違反了 Azure Resource Manager 搭配資源 URI 運作的方式。

Azure Cosmos DB 帳戶

下列各節會示範如何管理 Azure Cosmos DB 帳戶,包括:

建立 Azure Cosmos DB 帳戶

此命令會使用 多重區域服務管理容錯移轉和限定過期的一致性原則來建立 Azure Cosmos DB 資料庫的帳戶。

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$apiKind = "Sql"
$consistencyLevel = "BoundedStaleness"
$maxStalenessInterval = 300
$maxStalenessPrefix = 100000
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "East US" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "West US" -FailoverPriority 1 -IsZoneRedundant 0

New-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -LocationObject $locations `
    -Name $accountName `
    -ApiKind $apiKind `
    -EnableAutomaticFailover:$true `
    -DefaultConsistencyLevel $consistencyLevel `
    -MaxStalenessIntervalInSeconds $maxStalenessInterval `
    -MaxStalenessPrefix $maxStalenessPrefix
  • $resourceGroupName 要在其中部署 Azure Cosmos DB 帳戶的 Azure 資源群組。 此項目必須已經存在。
  • $locations 資料庫帳戶的區域,包含 FailoverPriority 0 的區域是寫入區域。
  • $accountName Azure Cosmos DB 帳戶名稱。 必須是唯一的、小寫的,且僅包含英數字元和 '-' 字元,且長度介於 3 到 31 個字元之間。
  • $apiKind 要建立的 Azure Cosmos DB 帳戶類型。 如需詳細資訊,請參閱 Azure Cosmos DB 中的 API
  • $consistencyPolicy$maxStalenessInterval$maxStalenessPrefix Azure Cosmos DB 帳戶的預設一致性層級與設定。 如需詳細資訊,請參閱 Azure Cosmos DB 的一致性層級

您可以為 Azure Cosmos DB 帳戶設定 IP 防火牆以、虛擬網路服務端點及私人端點。 如需如何為 Azure Cosmos DB 設定 IP 防火牆的相關資訊,請參閱設定 IP 防火牆。 如需如何為 Azure Cosmos DB 啟用服務端點的詳細資訊,請參閱設定從虛擬網路存取。 如需如何為 Azure Cosmos DB 啟用私人端點的詳細資訊,請參閱設定來自私人端點的存取

列出資源群組中的所有 Azure Cosmos DB 帳戶

此命令會列出資源群組中的所有 Azure Cosmos DB 帳戶。

$resourceGroupName = "myResourceGroup"

Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName

取得 Azure Cosmos DB 帳戶的屬性

此命令可讓您取得現有 Azure Cosmos DB 帳戶的屬性。

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName

建立 Azure Cosmos DB 帳戶

此命令可讓您更新 Azure Cosmos DB 資料庫帳戶屬性。 可更新的屬性如下:

  • 新增或移除區域
  • 變更預設的一致性原則
  • 變更 IP 範圍篩選條件
  • 變更虛擬網路組態
  • 啟用多重區域寫入

注意

您不能同時新增或移除區域 (locations),以及變更 Azure Cosmos DB 帳戶的其他屬性。 修改區域必須與帳戶的任何其他變更分開作業。

注意

此命令可讓您新增及移除區域,但不允許您修改容錯移轉優先順序或觸發手動容錯移轉。 請參閱修改容錯移轉優先順序觸發手動容錯移轉

提示

新增區域時,所有資料都必須完全複寫並認可到新的區域,才能將該區域標示為可用。 此作業所需的時間取決於帳戶中儲存的資料量。 如果非同步輸送量調整作業正在進行中,輸送量擴大規模作業將會暫停,並會在新增/移除區域作業完成時自動繼續。

# Create account with two regions
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$apiKind = "Sql"
$consistencyLevel = "Session"
$enableAutomaticFailover = $true
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "East US" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "West US" -FailoverPriority 1 -IsZoneRedundant 0

# Create the Azure Cosmos DB account
New-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -LocationObject $locations `
    -Name $accountName `
    -ApiKind $apiKind `
    -EnableAutomaticFailover:$enableAutomaticFailover `
    -DefaultConsistencyLevel $consistencyLevel

# Add a region to the account
$locationObject2 = @()
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "East US" -FailoverPriority 0 -IsZoneRedundant 0
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "West US" -FailoverPriority 1 -IsZoneRedundant 0
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "South Central US" -FailoverPriority 2 -IsZoneRedundant 0

Update-AzCosmosDBAccountRegion `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -LocationObject $locationObject2

Write-Host "Update-AzCosmosDBAccountRegion returns before the region update is complete."
Write-Host "Check account in Azure portal or using Get-AzCosmosDBAccount for region status."
Write-Host "When region was added, press any key to continue."
$HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
$HOST.UI.RawUI.Flushinputbuffer()

# Remove West US region from the account
$locationObject3 = @()
$locationObject3 += New-AzCosmosDBLocationObject -LocationName "East US" -FailoverPriority 0 -IsZoneRedundant 0
$locationObject3 += New-AzCosmosDBLocationObject -LocationName "South Central US" -FailoverPriority 1 -IsZoneRedundant 0

Update-AzCosmosDBAccountRegion `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -LocationObject $locationObject3

Write-Host "Update-AzCosmosDBAccountRegion returns before the region update is complete."
Write-Host "Check account in Azure portal or using Get-AzCosmosDBAccount for region status."

為 Azure Cosmos DB 帳戶啟用多個寫入區域

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$enableAutomaticFailover = $false
$enableMultiMaster = $true

# First disable service-managed failover - cannot have both service-managed
# failover and multi-region writes on an account
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableAutomaticFailover:$enableAutomaticFailover

# Now enable multi-region writes
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableMultipleWriteLocations:$enableMultiMaster

刪除 Azure Cosmos DB 帳戶

此命令會刪除現有的 Azure Cosmos DB 帳戶。

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Remove-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -PassThru:$true

更新 Azure Cosmos DB 帳戶的標籤

此命令會設定 Azure Cosmos DB 帳戶的 Azure 資源標籤。 標記可以在使用 New-AzCosmosDBAccount 建立帳戶時,以及使用 Update-AzCosmosDBAccount更新帳戶時設定。

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$tags = @{dept = "Finance"; environment = "Production";}

Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -Tag $tags

列出帳戶金鑰

當您建立 Azure Cosmos DB 帳戶時,服務會產生兩個主要存取金鑰,用於存取 Azure Cosmos DB 帳戶時的驗證。 也產生用於驗證唯讀作業的唯讀金鑰。 透過提供這兩個存取金鑰,Azure Cosmos DB 讓您可以重新產生及輪替金鑰 (一次一個),同時又不需中斷 Azure Cosmos DB 帳戶。 Azure Cosmos DB 帳戶有兩個讀寫金鑰 (主要和次要) 和兩個唯讀金鑰 (主要和次要)。

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzCosmosDBAccountKey `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -Type "Keys"

列出連接字串

下列命令會擷取連接字串,以將應用程式連線至 Azure Cosmos DB 帳戶。

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzCosmosDBAccountKey `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -Type "ConnectionStrings"

重新產生帳戶金鑰

請定期重新產生 Azure Cosmos DB 帳戶的存取金鑰,以便讓連線保持安全。 您可以對帳戶指派主要和次要存取金鑰。 這可讓用戶端在重新產生金鑰 (一次一個) 時保有存取能力。 Azure Cosmos DB 帳戶有四種金鑰 (主要、次要、主要唯讀和次要唯讀)

$resourceGroupName = "myResourceGroup" # Resource Group must already exist
$accountName = "mycosmosaccount" # Must be all lower case
$keyKind = "primary" # Other key kinds: secondary, primaryReadonly, secondaryReadonly

New-AzCosmosDBAccountKey `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -KeyKind $keyKind

啟用服務管理容錯移轉

下列命令會將 Azure Cosmos DB 帳戶設定為在主要區域變成無法使用時,執行服務受控容錯移轉到其次要區域。

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$enableAutomaticFailover = $true
$enableMultiMaster = $false

# First disable multi-region writes - cannot have both automatic
# failover and multi-region writes on an account
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableMultipleWriteLocations:$enableMultiMaster

# Now enable service-managed failover
Update-AzCosmosDBAccount `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -EnableAutomaticFailover:$enableAutomaticFailover

修改容錯移轉優先順序

針對已設定服務管理容錯移轉的帳戶,您可以變更順序,當主要複本無法使用時,Azure Cosmos DB 就會將次要複本升級為主要複本。

在下列範例中,我們假設目前的容錯移轉優先順序為 West US = 0East US = 1South Central US = 2。 此命令會將此變更為 West US = 0South Central US = 1East US = 2

警告

變更 failoverPriority=0 的位置會讓 Azure Cosmos DB 帳戶觸發手動容錯移轉。 變更其他優先順序則不會觸發容錯移轉。

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$locations = @("West US", "South Central US", "East US") # Regions ordered by UPDATED failover priority

Update-AzCosmosDBAccountFailoverPriority `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -FailoverPolicy $locations

觸發手動容錯移轉

對於已設定手動容錯移轉的帳戶,您可以藉由將任何次要複本修改為 failoverPriority=0,以容錯移轉及提升至主要複本。 此作業也可用來起始災害復原演練,以測試災害復原規劃。

在下列範例中,假設帳戶目前的容錯移轉優先順序為 West US = 0East US = 1,並翻轉區域。

警告

變更 failoverPriority=0locationName 會讓 Azure Cosmos DB 帳戶觸發手動容錯移轉。 變更其他優先順序則不會觸發容錯移轉。

注意

如果您在進行非同步調整規模作業時執行手動容錯移轉作業,則會暫停輸送量擴大規模作業。 當容錯移轉作業完成時,該作業便會自動繼續。

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$locations = @("East US", "West US") # Regions ordered by UPDATED failover priority

Update-AzCosmosDBAccountFailoverPriority `
    -ResourceGroupName $resourceGroupName `
    -Name $accountName `
    -FailoverPolicy $locations

列出 Azure Cosmos DB 帳戶上的資源鎖定

資源鎖定可以放在 Azure Cosmos DB 資源上,包括資料庫和集合。 下列範例顯示如何列出 Azure Cosmos DB 帳戶上的所有 Azure 資源鎖定。

$resourceGroupName = "myResourceGroup"
$resourceTypeAccount = "Microsoft.DocumentDB/databaseAccounts"
$accountName = "mycosmosaccount"

Get-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceTypeAccount `
    -ResourceName $accountName

Azure Cosmos DB 資料庫

下列各節會示範如何管理 Azure Cosmos DB 資料庫,包括:

建立 Azure Cosmos DB 資料庫

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

New-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName

使用共用輸送量,建立 Azure Cosmos DB 資料庫

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$databaseRUs = 400

New-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName `
    -Throughput $databaseRUs

取得 Azure Cosmos DB 資料庫的輸送量

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Get-AzCosmosDBSqlDatabaseThroughput `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName

移轉資料庫輸送量以自動調整

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Invoke-AzCosmosDBSqlDatabaseThroughputMigration `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName `
    -ThroughputType Autoscale

取得帳戶中的所有 Azure Cosmos DB 資料庫

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"

Get-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName

取得單一 Azure Cosmos DB 資料庫

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Get-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName

刪除 Azure Cosmos DB 資料庫

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Remove-AzCosmosDBSqlDatabase `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -Name $databaseName

在 Azure Cosmos DB 資料庫建立資源鎖定,防止刪除

$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$resourceName = "$accountName/$databaseName"
$lockName = "myResourceLock"
$lockLevel = "CanNotDelete"

New-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceType `
    -ResourceName $resourceName `
    -LockName $lockName `
    -LockLevel $lockLevel

移除 Azure Cosmos DB 資料庫的資源鎖定

$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$resourceName = "$accountName/$databaseName"
$lockName = "myResourceLock"

Remove-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceType `
    -ResourceName $resourceName `
    -LockName $lockName

Azure Cosmos DB 容器

下列各節會示範如何管理 Azure Cosmos DB 容器,包括:

建立 Azure Cosmos DB 容器

# Create an Azure Cosmos DB container with default indexes and throughput at 400 RU
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$throughput = 400 #minimum = 400

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -Throughput $throughput

建立具自動調整功能的 Azure Cosmos DB 容器

# Create an Azure Cosmos DB container with default indexes and autoscale throughput at 4000 RU
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$autoscaleMaxThroughput = 4000 #minimum = 4000

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -AutoscaleMaxThroughput $autoscaleMaxThroughput

使用大型分割區索引鍵大小,建立 Azure Cosmos DB 容器

# Create an Azure Cosmos DB container with a large partition key value (version = 2)
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -PartitionKeyVersion 2

取得 Azure Cosmos DB 容器的輸送量

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"

Get-AzCosmosDBSqlContainerThroughput `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName

移轉容器輸送量以自動調整

$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"

Invoke-AzCosmosDBSqlContainerThroughputMigration `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -ThroughputType Autoscale

使用自訂索引原則,建立 Azure Cosmos DB 容器

# Create a container with a custom indexing policy
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$indexPathIncluded = "/*"
$indexPathExcluded = "/myExcludedPath/*"

$includedPathIndex = New-AzCosmosDBSqlIncludedPathIndex -DataType String -Kind Range
$includedPath = New-AzCosmosDBSqlIncludedPath -Path $indexPathIncluded -Index $includedPathIndex

$indexingPolicy = New-AzCosmosDBSqlIndexingPolicy `
    -IncludedPath $includedPath `
    -ExcludedPath $indexPathExcluded `
    -IndexingMode Consistent `
    -Automatic $true

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -IndexingPolicy $indexingPolicy

建立已關閉編製索引的 Azure Cosmos DB 容器

# Create an Azure Cosmos DB container with no indexing
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"

$indexingPolicy = New-AzCosmosDBSqlIndexingPolicy `
    -IndexingMode None

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -IndexingPolicy $indexingPolicy

使用唯一索引鍵原則和 TTL,建立 Azure Cosmos DB 容器

# Create a container with a unique key policy and TTL of one day
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$uniqueKeyPath = "/myUniqueKeyPath"
$ttlInSeconds = 86400 # Set this to -1 (or don't use it at all) to never expire

$uniqueKey = New-AzCosmosDBSqlUniqueKey `
    -Path $uniqueKeyPath

$uniqueKeyPolicy = New-AzCosmosDBSqlUniqueKeyPolicy `
    -UniqueKey $uniqueKey

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -UniqueKeyPolicy $uniqueKeyPolicy `
    -TtlInSeconds $ttlInSeconds

使用衝突解析,建立 Azure Cosmos DB 容器

若要將所有衝突寫入到 ConflictsFeed 並分開處理,請傳遞 -Type "Custom" -Path ""

# Create container with last-writer-wins conflict resolution policy
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$conflictResolutionPath = "/myResolutionPath"

$conflictResolutionPolicy = New-AzCosmosDBSqlConflictResolutionPolicy `
    -Type LastWriterWins `
    -Path $conflictResolutionPath

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -ConflictResolutionPolicy $conflictResolutionPolicy

若要建立衝突解決原則以使用預存程序,請呼叫 New-AzCosmosDBSqlConflictResolutionPolicy 並傳遞 -Type-ConflictResolutionProcedure參數。

# Create container with custom conflict resolution policy using a stored procedure
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$partitionKeyPath = "/myPartitionKey"
$conflictResolutionSprocName = "mysproc"

$conflictResolutionSproc = "/dbs/$databaseName/colls/$containerName/sprocs/$conflictResolutionSprocName"

$conflictResolutionPolicy = New-AzCosmosDBSqlConflictResolutionPolicy `
    -Type Custom `
    -ConflictResolutionProcedure $conflictResolutionSproc

New-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName `
    -PartitionKeyKind Hash `
    -PartitionKeyPath $partitionKeyPath `
    -ConflictResolutionPolicy $conflictResolutionPolicy

列出資料庫中的所有 Azure Cosmos DB 容器

# List all Azure Cosmos DB containers in a database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"

Get-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName

取得資料庫中的單一 Azure Cosmos DB 容器

# Get a single Azure Cosmos DB container in a database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"

Get-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName

刪除 Azure Cosmos DB 容器

# Delete an Azure Cosmos DB container
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"

Remove-AzCosmosDBSqlContainer `
    -ResourceGroupName $resourceGroupName `
    -AccountName $accountName `
    -DatabaseName $databaseName `
    -Name $containerName

在 Azure Cosmos DB 容器上建立資源鎖定,以防刪除

$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$resourceName = "$accountName/$databaseName/$containerName"
$lockName = "myResourceLock"
$lockLevel = "CanNotDelete"

New-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceType `
    -ResourceName $resourceName `
    -LockName $lockName `
    -LockLevel $lockLevel

移除 Azure Cosmos DB 容器的資源鎖定

$resourceGroupName = "myResourceGroup"
$resourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
$resourceName = "$accountName/$databaseName/$containerName"
$lockName = "myResourceLock"

Remove-AzResourceLock `
    -ResourceGroupName $resourceGroupName `
    -ResourceType $resourceType `
    -ResourceName $resourceName `
    -LockName $lockName

下一步