param(
[parameter(Mandatory=$false)]
[string] $environmentName = "AzureCloud",
[parameter(Mandatory=$true)]
[string] $resourceGroupName,
[parameter(Mandatory=$true)]
[string] $MIName,
[parameter(Mandatory=$false)]
[string] $defaultEdition = "GeneralPurpose",
[parameter(Mandatory=$false)]
[string] $defaultvCores = "4",
[parameter(Mandatory=$false)]
[string] $defaultStorageSizeInGB = "32",
[parameter(Mandatory=$false)]
[string] $defaultComputeGeneration = "Gen5"
)
filter timestamp {"[$(Get-Date -Format G)]: $_"}
Write-Output "Script started." | timestamp
try
{
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process
# Connect to Azure with system-assigned managed identity
Connect-AzAccount -Identity
# set and store context
$AzureContext = Set-AzContext –SubscriptionId "put your SubscriptionId here"
Write-Output "Authenticated with Managed identity." | timestamp
}
catch
{
if (!$AzureContext)
{
$ErrorMessage = "Connection not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
# Get the MI object
try
{
$sqlMI = Get-AzSqlInstance -Name $MIName -ResourceGroupName $ResourceGroupName
Write-Output "MI name: $($sqlMI.ManagedInstanceName)" | timestamp
if ($sqlMI.ManagedInstanceName -ne "")
{
Write-Output "Current MI vCores: $($sqlMI.VCores), Storage: $($sqlMI.StorageSizeInGB)" | timestamp
Write-Output "---> Scaling schedule found. Check if current edition/tier is matching..." | timestamp
if($sqlMI.VCores -ne $defaultvCores -or $sqlMI.StorageSizeInGB -ne $defaultStorageSizeInGB)
{
Write-Output "--- ---> vCores and/or Storage Size are different. Changing!" | timestamp
Set-AzSqlInstance -Name $MIName -ResourceGroupName $ResourceGroupName -VCore $defaultvCores -StorageSizeInGB $defaultStorageSizeInGB -ComputeGeneration $defaultComputeGeneration -Edition $defaultEdition -Force -Confirm:$false | out-null
Write-Output "Change to vCores/Sotrage size as specified in scaling schedule initiated..." | timestamp
$sqlMI = Get-AzSqlInstance -Name $MIName -ResourceGroupName $ResourceGroupName
Write-Output "Current MI vCores: $($sqlMI.VCores), Storage: $($sqlMI.StorageSizeInGB)" | timestamp
}
else
{
Write-Output "Current MI vCores and/or Sotrage size matches the scaling schedule already. Exiting..." | timestamp
}
}
else{
Write-Error "Could not retrieve MI details" | timestamp
}
}
catch
{
Write-Error -Message $_.Exception
throw $_.Exception
}
Write-Output "Script finished." | timestamp