Skip to content

Instantly share code, notes, and snippets.

@hlx98007
Last active July 9, 2024 06:25
Show Gist options
  • Save hlx98007/984c507e1efc05079f22122ba3d9bdef to your computer and use it in GitHub Desktop.
Save hlx98007/984c507e1efc05079f22122ba3d9bdef to your computer and use it in GitHub Desktop.
# script taken from https://techcommunity.microsoft.com/t5/azure-database-support-blog/auto-scale-azure-sql-elastic-pools/ba-p/4011610
param (
[Parameter (Mandatory=$false)]
[object]$WebhookData
)
# Import the Az module
Import-Module Az.Sql
# If there is webhook data coming from an Azure Alert, go into the workflow.
if ($WebhookData) {
# Get the data object from WebhookData
$WebhookBody = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)
# Get the info needed to identify the SQL database (depends on the payload schema)
$schemaId = $WebhookBody.schemaId
Write-Verbose "schemaId: $schemaId" -Verbose
if ($schemaId -eq "azureMonitorCommonAlertSchema") {
# This is the common Metric Alert schema (released March 2019)
$Essentials = [object] ($WebhookBody.data).essentials
Write-Output $Essentials
# Get the first target only as this script doesn't handle multiple
$alertTargetIdArray = (($Essentials.alertTargetIds)[0]).Split("/")
$SubId = ($alertTargetIdArray)[2]
$ResourceGroupName = ($alertTargetIdArray)[4]
$ResourceType = ($alertTargetIdArray)[6] + "/" + ($alertTargetIdArray)[7]
$ServerName = ($alertTargetIdArray)[8]
$ElasticPoolName = ($alertTargetIdArray)[-1]
$status = $Essentials.monitorCondition
}
else {
# Schema not supported
Write-Error "The alert data schema - $schemaId - is not supported."
}
}
if (($status -eq "Activated") -or ($status -eq "Fired")) {
Write-Output "resourceType: $ResourceType"
Write-Output "serverName: $ServerName"
Write-Output "resourceGroupName: $ResourceGroupName"
Write-Output "subscriptionId: $SubId"
# Import the Az module
Import-Module Az.Sql
# Authenticate to Azure using Managed Identity
Connect-AzAccount -Identity
# Because Azure SQL tiers cannot be obtained programatically, we need to hardcode them as below.
# Check the values for your Azure SQL elastic pool and replace them within the below parameter $TargetDtu
$TargetDtu = @(50,100,200,300,400,800,1200,1600,2000,2500,3000)
$currentDatabaseDetails = Get-AzSqlElasticPool -ResourceGroupName $ResourceGroupName -ServerName $ServerName -ElasticPoolName $ElasticPoolName
#Replace the highest value with the values from your Azure elastic pool service tier
if ($currentDatabaseDetails.DTU -eq "3000") {
Write-Output "DTU database is already at highest tier (3000)"
}
else {
for ($i=0; $i -lt $TargetDtu.length; $i++) {
if ($TargetDtu[$i].equals($currentDatabaseDetails.DTU)) {
Set-AzSqlElasticPool -ResourceGroupName $ResourceGroupName -ServerName $ServerName -ElasticPoolName $ElasticPoolName -Dtu $TargetDtu[$i+1]
break
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment