PowerShell を使用して Azure Cosmos DB for NoSQL リソースを管理する

適用対象: NoSQL

以下のガイドでは、PowerShell を使用して、Azure Cosmos DB アカウント、データベース、コンテナー、スループットなど、Azure Cosmos DB for NoSQL リソースの管理をスクリプト処理および自動化する方法について説明します。 他の API 用の PowerShell コマンドレットについては、Cassandra 用 PowerShell サンプルMongoDB 用 API 用 PowerShell サンプルGremlin 用 PowerShell サンプルTable 用 PowerShell サンプルに関する記述を参照してください

Note

この記事のサンプルでは、Az.CosmosDB 管理コマンドレットを使用します。 最新の変更については、Az.CosmosDB API リファレンス ページを参照してください。

クロスプラットフォームの Azure Cosmos DB の管理には、クロスプラットフォームの PowerShell での Az コマンドレットと Az.CosmosDB コマンドレットのほか、Azure CLIREST APIAzure portal を使用できます。

注意

Azure を操作するには、Azure Az PowerShell モジュールを使用することをお勧めします。 作業を開始するには、Azure PowerShell のインストールに関する記事を参照してください。 Az PowerShell モジュールに移行する方法については、「AzureRM から Az への Azure PowerShell の移行」を参照してください。

作業の開始

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 範囲フィルターを変更する
  • 仮想ネットワーク構成を変更する
  • 複数リージョンの書き込みを有効にする

Note

Azure Cosmos DB アカウントに対し、リージョン (locations) の追加または削除と、他のプロパティの変更を同時に行うことはできません。 リージョンの変更は、アカウントに対する他のあらゆる変更とは別の操作として実行する必要があります。

Note

このコマンドでは、リージョンの追加および削除が可能ですが、フェールオーバー優先度を変更したり手動フェールオーバーをトリガーしたりすることはできません。 「フェールオーバー優先度を変更する」と「手動フェールオーバーをトリガーする」を参照してください。

ヒント

新しいリージョンが追加されるとき、すべてのデータが新しいリージョンに完全にレプリケートおよびコミットされるまで、リージョンには使用可能のマークが付きません。 この操作にかかる時間は、アカウント内に保存されているデータの量によって変動します。 非同期スループット スケーリング操作が進行中の場合、スループットのスケールアップ操作は一時停止され、リージョンの追加または削除操作が完了すると自動的に再開されます。

# 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 アカウントを作成すると、2 つのプライマリ アクセス キーが生成されます。これらのアクセス キーは、Azure Cosmos DB アカウントにアクセスする際の認証に使用できます。 また、読み取り専用操作を認証するための読み取り専用キーも生成されます。 アクセス キーが 2 つ提供されるため、Azure Cosmos DB アカウントを中断することなく、キーの再生成とローテーションを 1 つずつ行うことができます。 Azure Cosmos DB アカウントには、2 つの読み取り/書き込みキー (プライマリおよびセカンダリ) と、2 つの読み取り専用キー (プライマリおよびセカンダリ) が存在します。

$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 アカウントへのアクセス キーは、接続を安全に保つために定期的に再生成する必要があります。 アカウントにはプライマリ アクセス キーとセカンダリ アクセス キーが割り当てられます。 これにより、キーを 1 つずつ再生成し、その間も、クライアントはアクセスを維持できます。 Azure Cosmos DB アカウントには、4 種類のキー (Primary、Secondary、PrimaryReadonly、および SecondaryReadonly) があります。

$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 = 0 および East US = 1 であるものとして、リージョンを反転させます。

注意

failoverPriority=0 に対する locationName を変更すると、Azure Cosmos DB アカウントの手動フェールオーバーがトリガーされます。 他の優先度を変更しても、フェールオーバーはトリガーされません。

Note

非同期スループット スケーリング操作の進行中に手動フェールオーバー操作を実行すると、スループットのスケールアップ操作は一時停止されます。 フェールオーバー操作が完了すると、自動的に再開されます。

$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

データベース内の 1 つの 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

次のステップ