Manage Azure Cosmos DB for NoSQL resources using PowerShell
APPLIES TO: NoSQL
The following guide describes how to use PowerShell to script and automate management of Azure Cosmos DB for NoSQL resources, including the Azure Cosmos DB account, database, container, and throughput. For PowerShell cmdlets for other APIs see PowerShell Samples for Cassandra, PowerShell Samples for API for MongoDB, PowerShell Samples for Gremlin, PowerShell Samples for Table
Note
Samples in this article use Az.CosmosDB management cmdlets. See the Az.CosmosDB API reference page for the latest changes.
For cross-platform management of Azure Cosmos DB, you can use the Az
and Az.CosmosDB
cmdlets with cross-platform PowerShell, as well as the Azure CLI, the REST API, or the Azure portal.
Note
We recommend that you use the Azure Az PowerShell module to interact with Azure. See Install Azure PowerShell to get started. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
Getting Started
Follow the instructions in How to install and configure Azure PowerShell to install and sign in to your Azure account in PowerShell.
Important
Azure Cosmos DB resources cannot be renamed as this violates how Azure Resource Manager works with resource URIs.
Azure Cosmos DB accounts
The following sections demonstrate how to manage the Azure Cosmos DB account, including:
- Create an Azure Cosmos DB account
- Update an Azure Cosmos DB account
- List all Azure Cosmos DB accounts in a subscription
- Get an Azure Cosmos DB account
- Delete an Azure Cosmos DB account
- Update tags for an Azure Cosmos DB account
- List keys for an Azure Cosmos DB account
- Regenerate keys for an Azure Cosmos DB account
- List connection strings for an Azure Cosmos DB account
- Modify failover priority for an Azure Cosmos DB account
- Trigger a manual failover for an Azure Cosmos DB account
- List resource locks on an Azure Cosmos DB account
Create an Azure Cosmos DB account
This command creates an Azure Cosmos DB database account with multiple regions, service-managed failover and bounded-staleness consistency policy.
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$apiKind = "Sql"
$consistencyLevel = "BoundedStaleness"
$maxStalenessInterval = 300
$maxStalenessPrefix = 100000
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "China East" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "China North" -FailoverPriority 1 -IsZoneRedundant 0
New-AzCosmosDBAccount `
-ResourceGroupName $resourceGroupName `
-LocationObject $locations `
-Name $accountName `
-ApiKind $apiKind `
-EnableAutomaticFailover:$true `
-DefaultConsistencyLevel $consistencyLevel `
-MaxStalenessIntervalInSeconds $maxStalenessInterval `
-MaxStalenessPrefix $maxStalenessPrefix
$resourceGroupName
The Azure resource group into which to deploy the Azure Cosmos DB account. It must already exist.$locations
The regions for the database account, the region withFailoverPriority 0
is the write region.$accountName
The name for the Azure Cosmos DB account. Must be unique, lowercase, include only alphanumeric and '-' characters, and between 3 and 31 characters in length.$apiKind
The type of Azure Cosmos DB account to create. For more information, see APIs in Azure Cosmos DB.$consistencyPolicy
,$maxStalenessInterval
, and$maxStalenessPrefix
The default consistency level and settings of the Azure Cosmos DB account. For more information, see Consistency Levels in Azure Cosmos DB.
Azure Cosmos DB accounts can be configured with IP Firewall, Virtual Network service endpoints, and private endpoints. For information on how to configure the IP Firewall for Azure Cosmos DB, see Configure IP Firewall. For information on how to enable service endpoints for Azure Cosmos DB, see Configure access from virtual Networks. For information on how to enable private endpoints for Azure Cosmos DB, see Configure access from private endpoints.
List all Azure Cosmos DB accounts in a Resource Group
This command lists all Azure Cosmos DB accounts in a Resource Group.
$resourceGroupName = "myResourceGroup"
Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName
Get the properties of an Azure Cosmos DB account
This command allows you to get the properties of an existing Azure Cosmos DB account.
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
Get-AzCosmosDBAccount -ResourceGroupName $resourceGroupName -Name $accountName
Update an Azure Cosmos DB account
This command allows you to update your Azure Cosmos DB database account properties. Properties that can be updated include the following:
- Adding or removing regions
- Changing default consistency policy
- Changing IP Range Filter
- Changing Virtual Network configurations
- Enabling multi-region writes
Note
You cannot simultaneously add or remove regions (locations
) and change other properties for an Azure Cosmos DB account. Modifying regions must be performed as a separate operation from any other change to the account.
Note
This command allows you to add and remove regions but does not allow you to modify failover priorities or trigger a manual failover. See Modify failover priority and Trigger manual failover.
Tip
When a new region is added, all data must be fully replicated and committed into the new region before the region is marked as available. The amount of time this operation takes will depend upon how much data is stored within the account. If an asynchronous throughput scaling operation is in progress, the throughput scale-up operation will be paused and will resume automatically when the add/remove region operation is complete.
# Create account with two regions
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$apiKind = "Sql"
$consistencyLevel = "Session"
$enableAutomaticFailover = $true
$locations = @()
$locations += New-AzCosmosDBLocationObject -LocationName "China East" -FailoverPriority 0 -IsZoneRedundant 0
$locations += New-AzCosmosDBLocationObject -LocationName "China North" -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 "China East" -FailoverPriority 0 -IsZoneRedundant 0
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "China North" -FailoverPriority 1 -IsZoneRedundant 0
$locationObject2 += New-AzCosmosDBLocationObject -LocationName "China East" -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 China North region from the account
$locationObject3 = @()
$locationObject3 += New-AzCosmosDBLocationObject -LocationName "China East" -FailoverPriority 0 -IsZoneRedundant 0
$locationObject3 += New-AzCosmosDBLocationObject -LocationName "China East" -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."
Enable multiple write regions for an Azure Cosmos DB account
$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
Delete an Azure Cosmos DB account
This command deletes an existing Azure Cosmos DB account.
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
Remove-AzCosmosDBAccount `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-PassThru:$true
Update Tags of an Azure Cosmos DB account
This command sets the Azure resource tags for an Azure Cosmos DB account. Tags can be set both at account creation using New-AzCosmosDBAccount
as well as on account update using Update-AzCosmosDBAccount
.
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$tags = @{dept = "Finance"; environment = "Production";}
Update-AzCosmosDBAccount `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-Tag $tags
List Account Keys
When you create an Azure Cosmos DB account, the service generates two primary access keys that can be used for authentication when the Azure Cosmos DB account is accessed. Read-only keys for authenticating read-only operations are also generated. By providing two access keys, Azure Cosmos DB enables you to regenerate and rotate one key at a time with no interruption to your Azure Cosmos DB account. Azure Cosmos DB accounts have two read-write keys (primary and secondary) and two read-only keys (primary and secondary).
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
Get-AzCosmosDBAccountKey `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-Type "Keys"
List Connection Strings
The following command retrieves connection strings to connect apps to the Azure Cosmos DB account.
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
Get-AzCosmosDBAccountKey `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-Type "ConnectionStrings"
Regenerate Account Keys
Access keys to an Azure Cosmos DB account should be periodically regenerated to help keep connections secure. A primary and secondary access keys are assigned to the account. This allows clients to maintain access while one key at a time is regenerated. There are four types of keys for an Azure Cosmos DB account (Primary, Secondary, PrimaryReadonly, and 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
Enable service-managed failover
The following command sets an Azure Cosmos DB account to perform a service-managed fail over to its secondary region should the primary region become unavailable.
$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
Modify Failover Priority
For accounts configured with Service-Managed Failover, you can change the order in which Azure Cosmos DB will promote secondary replicas to primary should the primary become unavailable.
For the example below, assume the current failover priority is China North = 0
, China East = 1
, China East = 2
. The command will change this to China North = 0
, China East = 1
, China East = 2
.
Caution
Changing the location for failoverPriority=0
will trigger a manual failover for an Azure Cosmos DB account. Any other priority changes will not trigger a failover.
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$locations = @("China North", "China East", "China East") # Regions ordered by UPDATED failover priority
Update-AzCosmosDBAccountFailoverPriority `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-FailoverPolicy $locations
Trigger Manual Failover
For accounts configured with Manual Failover, you can fail over and promote any secondary replica to primary by modifying to failoverPriority=0
. This operation can be used to initiate a disaster recovery drill to test disaster recovery planning.
For the example below, assume the account has a current failover priority of China North = 0
and China East = 1
and flip the regions.
Caution
Changing locationName
for failoverPriority=0
will trigger a manual failover for an Azure Cosmos DB account. Any other priority change will not trigger a failover.
Note
If you perform a manual failover operation while an asynchronous throughput scaling operation is in progress, the throughput scale-up operation will be paused. It will resume automatically when the failover operation is complete.
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$locations = @("China East", "China North") # Regions ordered by UPDATED failover priority
Update-AzCosmosDBAccountFailoverPriority `
-ResourceGroupName $resourceGroupName `
-Name $accountName `
-FailoverPolicy $locations
List resource locks on an Azure Cosmos DB account
Resource locks can be placed on Azure Cosmos DB resources including databases and collections. The example below shows how to list all Azure resource locks on an Azure Cosmos DB account.
$resourceGroupName = "myResourceGroup"
$resourceTypeAccount = "Microsoft.DocumentDB/databaseAccounts"
$accountName = "mycosmosaccount"
Get-AzResourceLock `
-ResourceGroupName $resourceGroupName `
-ResourceType $resourceTypeAccount `
-ResourceName $accountName
Azure Cosmos DB Database
The following sections demonstrate how to manage the Azure Cosmos DB database, including:
- Create an Azure Cosmos DB database
- Create an Azure Cosmos DB database with shared throughput
- Get the throughput of an Azure Cosmos DB database
- Migrate database throughput to autoscale
- List all Azure Cosmos DB databases in an account
- Get a single Azure Cosmos DB database
- Delete an Azure Cosmos DB database
- Create a resource lock on an Azure Cosmos DB database to prevent delete
- Remove a resource lock on an Azure Cosmos DB database
Create an Azure Cosmos DB database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
New-AzCosmosDBSqlDatabase `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-Name $databaseName
Create an Azure Cosmos DB database with shared throughput
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$databaseRUs = 400
New-AzCosmosDBSqlDatabase `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-Name $databaseName `
-Throughput $databaseRUs
Get the throughput of an Azure Cosmos DB database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
Get-AzCosmosDBSqlDatabaseThroughput `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-Name $databaseName
Migrate database throughput to autoscale
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
Invoke-AzCosmosDBSqlDatabaseThroughputMigration `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-Name $databaseName `
-ThroughputType Autoscale
Get all Azure Cosmos DB databases in an account
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
Get-AzCosmosDBSqlDatabase `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName
Get a single Azure Cosmos DB database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
Get-AzCosmosDBSqlDatabase `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-Name $databaseName
Delete an Azure Cosmos DB database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
Remove-AzCosmosDBSqlDatabase `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-Name $databaseName
Create a resource lock on an Azure Cosmos DB database to prevent delete
$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
Remove a resource lock on an Azure Cosmos DB database
$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 Container
The following sections demonstrate how to manage the Azure Cosmos DB container, including:
- Create an Azure Cosmos DB container
- Create an Azure Cosmos DB container with autoscale
- Create an Azure Cosmos DB container with a large partition key
- Get the throughput of an Azure Cosmos DB container
- Migrate container throughput to autoscale
- Create an Azure Cosmos DB container with custom indexing
- Create an Azure Cosmos DB container with indexing turned off
- Create an Azure Cosmos DB container with unique key and TTL
- Create an Azure Cosmos DB container with conflict resolution
- List all Azure Cosmos DB containers in a database
- Get a single Azure Cosmos DB container in a database
- Delete an Azure Cosmos DB container
- Create a resource lock on an Azure Cosmos DB container to prevent delete
- Remove a resource lock on an Azure Cosmos DB container
Create an Azure Cosmos DB container
# 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
Create an Azure Cosmos DB container with autoscale
# 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
Create an Azure Cosmos DB container with a large partition key size
# 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
Get the throughput of an Azure Cosmos DB container
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
Get-AzCosmosDBSqlContainerThroughput `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName
Migrate container throughput to autoscale
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
Invoke-AzCosmosDBSqlContainerThroughputMigration `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName `
-ThroughputType Autoscale
Create an Azure Cosmos DB container with custom index policy
# 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
Create an Azure Cosmos DB container with indexing turned off
# 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
Create an Azure Cosmos DB container with unique key policy and TTL
# 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
Create an Azure Cosmos DB container with conflict resolution
To write all conflicts to the ConflictsFeed and handle separately, pass -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
To create a conflict resolution policy to use a stored procedure, call New-AzCosmosDBSqlConflictResolutionPolicy
and pass parameters -Type
and -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
List all Azure Cosmos DB containers in a database
# List all Azure Cosmos DB containers in a database
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
Get-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName
Get a single Azure Cosmos DB container in a database
# 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
Delete an Azure Cosmos DB container
# Delete an Azure Cosmos DB container
$resourceGroupName = "myResourceGroup"
$accountName = "mycosmosaccount"
$databaseName = "myDatabase"
$containerName = "myContainer"
Remove-AzCosmosDBSqlContainer `
-ResourceGroupName $resourceGroupName `
-AccountName $accountName `
-DatabaseName $databaseName `
-Name $containerName
Create a resource lock on an Azure Cosmos DB container to prevent delete
$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
Remove a resource lock on an Azure Cosmos DB container
$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