Skip to content

Commit 088beea

Browse files
authored
Merge pull request #113275 from plzm/cosmosdb-manage-with-powershell
Cosmos DB: Update manage-with-powershell.md article for Az.CosmosDB 0.1.4 release
2 parents 30e0134 + edbac6f commit 088beea

File tree

1 file changed

+67
-42
lines changed

1 file changed

+67
-42
lines changed

articles/cosmos-db/manage-with-powershell.md

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Use Azure Powershell manage your Azure Cosmos accounts, databases,
44
author: markjbrown
55
ms.service: cosmos-db
66
ms.topic: sample
7-
ms.date: 03/26/2020
7+
ms.date: 04/29/2020
88
ms.author: mjbrown
99
ms.custom: seodec18
1010
---
@@ -14,9 +14,7 @@ ms.custom: seodec18
1414
The following guide describes how to use PowerShell to script and automate management of Azure Cosmos DB resources, including account, database, container, and throughput.
1515

1616
> [!NOTE]
17-
> Samples in this article use `Get-AzResource` and `Set-AzResource` Powershell cmdlets for Azure resource operations as well as [Az.CosmosDB](https://docs.microsoft.com/powershell/module/az.cosmosdb) management cmdlets. `Az.CosmosDB` cmdlets are still in preview and may change before they are Generally Available. See the [Az.CosmosDB](https://docs.microsoft.com/powershell/module/az.cosmosdb) API reference page for any updates to the commands.
18-
19-
To view all of the properties that can be managed using `Get-Resource`/`Set-AzResource` PowerShell cmdlets, see [Azure Cosmos DB resource provider schema](/azure/templates/microsoft.documentdb/allversions)
17+
> Samples in this article use [Az.CosmosDB](https://docs.microsoft.com/powershell/module/az.cosmosdb) management cmdlets. These cmdlets are still in preview and may change before they are Generally Available. See the [Az.CosmosDB](https://docs.microsoft.com/powershell/module/az.cosmosdb) API reference page for any updates to the commands.
2018
2119
For cross-platform management of Azure Cosmos DB, you can use the `Az` and `Az.CosmosDB` cmdlets with [cross-platform Powershell](https://docs.microsoft.com/powershell/scripting/install/installing-powershell), as well as the [Azure CLI](manage-with-cli.md), the [REST API][rp-rest-api], or the [Azure portal](create-sql-api-dotnet.md#create-account).
2220

@@ -26,8 +24,6 @@ For cross-platform management of Azure Cosmos DB, you can use the `Az` and `Az.C
2624

2725
Follow the instructions in [How to install and configure Azure PowerShell][powershell-install-configure] to install and sign in to your Azure account in Powershell.
2826

29-
* `Set-AzureResource` is used below. It will ask for user confirmation. If you prefer to execute without requiring user confirmation, append the `-Force` flag to the command.
30-
3127
## Azure Cosmos accounts
3228

3329
The following sections demonstrate how to manage the Azure Cosmos account, including:
@@ -52,14 +48,17 @@ This command creates an Azure Cosmos DB database account with [multiple regions]
5248
$resourceGroupName = "myResourceGroup"
5349
$locations = @("West US 2", "East US 2")
5450
$accountName = "mycosmosaccount"
55-
$apiKind = "GlobalDocumentDB"
51+
$apiKind = "Sql"
5652
$consistencyLevel = "BoundedStaleness"
5753
$maxStalenessInterval = 300
5854
$maxStalenessPrefix = 100000
5955
60-
New-AzCosmosDBAccount -ResourceGroupName $resourceGroupName `
61-
-Location $locations -Name $accountName `
62-
-ApiKind $apiKind -EnableAutomaticFailover:$true `
56+
New-AzCosmosDBAccount `
57+
-ResourceGroupName $resourceGroupName `
58+
-Location $locations `
59+
-Name $accountName `
60+
-ApiKind $apiKind `
61+
-EnableAutomaticFailover:$true `
6362
-DefaultConsistencyLevel $consistencyLevel `
6463
-MaxStalenessIntervalInSeconds $maxStalenessInterval `
6564
-MaxStalenessPrefix $maxStalenessPrefix
@@ -105,7 +104,7 @@ This command allows you to update your Azure Cosmos DB database account properti
105104
* Enabling Multi-master
106105

107106
> [!NOTE]
108-
> You cannot simultaneously add or remove regions `locations` and change other properties for an Azure Cosmos account. Modifying regions must be performed as a separate operation from any other change to the account.
107+
> You cannot simultaneously add or remove regions (`locations`) and change other properties for an Azure Cosmos account. Modifying regions must be performed as a separate operation from any other change to the account.
109108
> [!NOTE]
110109
> 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](#modify-failover-priority) and [Trigger manual failover](#trigger-manual-failover).
111110
@@ -114,37 +113,33 @@ This command allows you to update your Azure Cosmos DB database account properti
114113
$resourceGroupName = "myResourceGroup"
115114
$locations = @("West US 2", "East US 2")
116115
$accountName = "mycosmosaccount"
117-
$apiKind = "GlobalDocumentDB"
116+
$apiKind = "Sql"
118117
$consistencyLevel = "Session"
119118
$enableAutomaticFailover = $true
120119
120+
# Create the Cosmos DB account
121121
New-AzCosmosDBAccount `
122122
-ResourceGroupName $resourceGroupName `
123-
-Location $locations -Name $accountName `
124-
-ApiKind $apiKind -EnableAutomaticFailover:$enableAutomaticFailover `
123+
-Location $locations `
124+
-Name $accountName `
125+
-ApiKind $apiKind `
126+
-EnableAutomaticFailover:$enableAutomaticFailover `
125127
-DefaultConsistencyLevel $consistencyLevel
126128
127-
# Region operations
128-
$resourceType = "Microsoft.DocumentDb/databaseAccounts"
129-
$apiVersion = "2020-03-01"
130-
131129
# Add a region to the account
132130
$locations2 = @("West US 2", "East US 2", "South Central US")
133131
$locationObjects2 = @()
134132
$i = 0
135-
ForEach ($location in $locations2) { $locationObjects2 += @{ locationName = "$location"; failoverPriority = $i++ } }
136-
$accountProperties = @{
137-
databaseAccountOfferType = "Standard";
138-
locations = $locationObjects2;
139-
enableAutomaticFailover = $enableAutomaticFailover;
133+
ForEach ($location in $locations2) {
134+
$locationObjects2 += @{ locationName = "$location"; failoverPriority = $i++ }
140135
}
141136
142-
Set-AzResource -ResourceType $resourceType `
137+
Update-AzCosmosDBAccountRegion `
143138
-ResourceGroupName $resourceGroupName `
144-
-ApiVersion $apiVersion -Name $accountName `
145-
-PropertyObject $accountProperties
139+
-Name $accountName `
140+
-LocationObject $locationObjects2
146141
147-
Write-Host "Set-AzResource returns before the region update is complete."
142+
Write-Host "Update-AzCosmosDBAccountRegion returns before the region update is complete."
148143
Write-Host "Check account in Azure portal or using Get-AzCosmosDBAccount for region status."
149144
Write-Host "When region was added, press any key to continue."
150145
$HOST.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | OUT-NULL
@@ -154,19 +149,16 @@ $HOST.UI.RawUI.Flushinputbuffer()
154149
$locations3 = @("West US 2", "South Central US")
155150
$locationObjects3 = @()
156151
$i = 0
157-
ForEach ($location in $locations3) { $locationObjects3 += @{ locationName = "$location"; failoverPriority = $i++ } }
158-
$accountProperties = @{
159-
databaseAccountOfferType = "Standard";
160-
locations = $locationObjects3;
161-
enableAutomaticFailover = $enableAutomaticFailover;
152+
ForEach ($location in $locations3) {
153+
$locationObjects3 += @{ locationName = "$location"; failoverPriority = $i++ }
162154
}
163155
164-
Set-AzResource -ResourceType $resourceType `
156+
Update-AzCosmosDBAccountRegion `
165157
-ResourceGroupName $resourceGroupName `
166-
-ApiVersion $apiVersion -Name $accountName `
167-
-PropertyObject $accountProperties
158+
-Name $accountName `
159+
-LocationObject $locationObjects3
168160
169-
Write-Host "Set-AzResource returns before the region update is complete."
161+
Write-Host "Update-AzCosmosDBAccountRegion returns before the region update is complete."
170162
Write-Host "Check account in Azure portal or using Get-AzCosmosDBAccount for region status."
171163
```
172164
### <a id="multi-master"></a> Enable multiple write regions for an Azure Cosmos account
@@ -201,7 +193,8 @@ $accountName = "mycosmosaccount"
201193
202194
Remove-AzCosmosDBAccount `
203195
-ResourceGroupName $resourceGroupName `
204-
-Name $accountName -PassThru
196+
-Name $accountName `
197+
-PassThru:$true
205198
```
206199

207200
### <a id="update-tags"></a> Update Tags of an Azure Cosmos account
@@ -215,7 +208,8 @@ $tags = @{dept = "Finance"; environment = "Production";}
215208
216209
Update-AzCosmosDBAccount `
217210
-ResourceGroupName $resourceGroupName `
218-
-Name $accountName -Tag $tags
211+
-Name $accountName `
212+
-Tag $tags
219213
```
220214

221215
### <a id="list-keys"></a> List Account Keys
@@ -230,7 +224,8 @@ $accountName = "mycosmosaccount"
230224
231225
Get-AzCosmosDBAccountKey `
232226
-ResourceGroupName $resourceGroupName `
233-
-Name $accountName -Type "Keys"
227+
-Name $accountName `
228+
-Type "Keys"
234229
```
235230

236231
### <a id="list-connection-strings"></a> List Connection Strings
@@ -243,7 +238,8 @@ $accountName = "mycosmosaccount"
243238
244239
Get-AzCosmosDBAccountKey `
245240
-ResourceGroupName $resourceGroupName `
246-
-Name $accountName -Type "ConnectionStrings"
241+
-Name $accountName `
242+
-Type "ConnectionStrings"
247243
```
248244

249245
### <a id="regenerate-keys"></a> Regenerate Account Keys
@@ -258,7 +254,8 @@ $keyKind = "primary" # Other key kinds: secondary, primaryReadOnly, secondaryRea
258254
259255
New-AzCosmosDBAccountKey `
260256
-ResourceGroupName $resourceGroupName `
261-
-Name $accountName -KeyKind $keyKind
257+
-Name $accountName `
258+
-KeyKind $keyKind
262259
```
263260

264261
### <a id="enable-automatic-failover"></a> Enable automatic failover
@@ -568,7 +565,7 @@ Set-AzCosmosDBSqlContainer `
568565

569566
### <a id="create-container-lww"></a>Create an Azure Cosmos DB container with conflict resolution
570567

571-
To create a conflict resolution policy to use a stored procedure, set `"mode"="custom"` and set the resolution path as the name of the stored procedure, `"conflictResolutionPath"="myResolverStoredProcedure"`. To write all conflicts to the ConflictsFeed and handle separately, set `"mode"="custom"` and `"conflictResolutionPath"=""`
568+
To write all conflicts to the ConflictsFeed and handle separately, pass `-Type "Custom" -Path ""`.
572569

573570
```azurepowershell-interactive
574571
# Create container with last-writer-wins conflict resolution policy
@@ -593,6 +590,34 @@ Set-AzCosmosDBSqlContainer `
593590
-ConflictResolutionPolicy $conflictResolutionPolicy
594591
```
595592

593+
To create a conflict resolution policy to use a stored procedure, call `New-AzCosmosDBSqlConflictResolutionPolicy` and pass parameters `-Type` and `-ConflictResolutionProcedure`.
594+
595+
```azurepowershell-interactive
596+
# Create container with custom conflict resolution policy using a stored procedure
597+
$resourceGroupName = "myResourceGroup"
598+
$accountName = "mycosmosaccount"
599+
$databaseName = "myDatabase"
600+
$containerName = "myContainer"
601+
$partitionKeyPath = "/myPartitionKey"
602+
$conflictResolutionSprocName = "mysproc"
603+
604+
$conflictResolutionSproc = "/dbs/$databaseName/colls/$containerName/sprocs/$conflictResolutionSprocName"
605+
606+
$conflictResolutionPolicy = New-AzCosmosDBSqlConflictResolutionPolicy `
607+
-Type Custom `
608+
-ConflictResolutionProcedure $conflictResolutionSproc
609+
610+
Set-AzCosmosDBSqlContainer `
611+
-ResourceGroupName $resourceGroupName `
612+
-AccountName $accountName `
613+
-DatabaseName $databaseName `
614+
-Name $containerName `
615+
-PartitionKeyKind Hash `
616+
-PartitionKeyPath $partitionKeyPath `
617+
-ConflictResolutionPolicy $conflictResolutionPolicy
618+
```
619+
620+
596621
### <a id="list-containers"></a>List all Azure Cosmos DB containers in a database
597622

598623
```azurepowershell-interactive

0 commit comments

Comments
 (0)