Scale-CosmosDB.ps1
<#PSScriptInfo
.VERSION 1.0 .GUID e824788f-6b67-8b5d-ba3f-cf754aac6e49 .AUTHOR Alex Drenea .COMPANYNAME Alex Drenea .COPYRIGHT .TAGS CosmosDB Azure .LICENSEURI https://www.apache.org/licenses/LICENSE-2.0.txt .PROJECTURI https://gist.github.com/alexdrenea/e824788f6b678b5dba3fcf754aac6e49 .ICONURI https://alexdrenea.com/css/images/logo.png .EXTERNALMODULEDEPENDENCIES Az .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES The script provides the ability to scale an Azure CosmosDb database. #> <# .DESCRIPTION The script provides the ability to scale an Azure CosmosDb database. #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseApprovedVerbs", "")] param( [Parameter(Mandatory = $true)] [string] $ResourceGroupName, [Parameter(Mandatory = $true)] [string] $AccountName, [Parameter(Mandatory = $true)] [string] $DatabaseName, [Parameter(Mandatory = $true)] [string] $ContainerName, [Parameter(Mandatory = $true, HelpMessage="Values between 400 and 1000000 inclusive in increments of 100")] [int] $Throughput ) $connectionName = "AzureRunAsConnection" try { $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName Connect-AzAccount ` -ServicePrincipal ` -TenantId $servicePrincipalConnection.TenantId ` -ApplicationId $servicePrincipalConnection.ApplicationId ` -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint } catch { if (!$servicePrincipalConnection) { $ErrorMessage = "Connection $connectionName not found." throw $ErrorMessage } else { Write-Error -Message $_.Exception throw $_.Exception } } if ( $Throughput -lt 400 -or $Throughput -gt 1000000 -or ($Throughput % 100) -ne 0 ){ $message = "Invalid Throughput $Throughput. Values between 400 and 1000000 inclusive in increments of 100" Write-Error -Message $message return } $ErrorActionPreference = "Continue" $p = Get-AzResource ` -ResourceType "Microsoft.DocumentDb/databaseAccounts" -ApiVersion "2016-03-31" ` -ResourceGroupName $ResourceGroupName -Name $accountName ` | Select-Object -expand Properties $accountType = $p.EnabledApiTypes $resourceType = '' $resourceName = '' if ($accountType -match 'Table'){ $resourceType = 'Microsoft.DocumentDb/databaseAccounts/apis/tables/settings' $resourceName = $accountName + "/table/" + $ContainerName + "/throughput" } elseif ($accountType -match 'MongoDB'){ $resourceType = 'Microsoft.DocumentDb/databaseAccounts/apis/databases/collections/settings'; $resourceName = $accountName + "/mongodb/" + $DatabaseName + "/" + $ContainerName + "/throughput" } elseif ($accountType -match 'Gremlin'){ $resourceType = 'Microsoft.DocumentDb/databaseAccounts/apis/databases/graphs/settings'; $resourceName = $accountName + "/gremlin/" + $DatabaseName + "/" + $ContainerName + "/throughput" } elseif ($accountType -match 'Cassandra'){ $resourceType = 'Microsoft.DocumentDB/databaseAccounts/apis/keyspaces/tables/settings'; $resourceName = $accountName + "/cassandra/" + $DatabaseName + "/" + $ContainerName + "/throughput" } elseif ($accountType -match 'Sql'){ $resourceType = 'Microsoft.DocumentDb/databaseAccounts/apis/databases/containers/settings'; $resourceName = $accountName + "/sql/" + $DatabaseName + "/" + $ContainerName + "/throughput" } else{ $message = "Unsupported CosmosDB account type '$accountType'. Supported APIs are: SQL, Gremlin, MongoDB, Table and Cassandra." Write-Error -Message $message return } $properties = @{ "resource"=@{"throughput"=$throughput} } try { Set-AzResource ` -ResourceType $resourceType -ApiVersion "2016-03-31" ` -ResourceGroupName $ResourceGroupName -Name $resourceName ` -PropertyObject $properties ` -Force } catch { Write-Error -Message $_.Exception throw $_.Exception } |