Created
December 20, 2016 14:54
-
-
Save dazfuller/859d2b2afd20bb59b115df3a8264e7cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.DESCRIPTION | |
A runbook which will scale down different types of resources for each resource group | |
.NOTES | |
AUTHOR: @dazfuller | |
LASTEDIT: Dec 20, 2016 | |
#> | |
$connectionName = "AzureRunAsConnection" | |
try | |
{ | |
# Get the connection "AzureRunAsConnection " | |
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName | |
"Logging in to Azure..." | |
Login-AzureRmAccount ` | |
-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 | |
} | |
} | |
# Scales databases and data warehouses in a resource group down to the lowest standard tier pricing plan | |
function ScaleDatabases { | |
Param ( | |
$ResourceGroup | |
) | |
foreach ($SqlServer in Get-AzureRmSqlServer -ResourceGroupName $ResourceGroup.ResourceGroupName) { | |
foreach ($Database in $SqlServer | Get-AzureRmSqlDatabase |? DatabaseName -ne "master") { | |
# Get the database information again, running without database name doesn't return tags | |
$Database = $Database | Get-AzureRmSqlDatabase | |
# Check to see if the database has been tagged to maintain the existing scale | |
if ($Database.Tags.Keys -contains "keepScale" -and $Database.Tags["keepScale"] -eq $true) { | |
Write-Output "Database $($Database.ResourceId) has been flagged to maintain the existing scale" | |
continue | |
} | |
# Capture the current scale information | |
$CurrentPricingTier = $Database.CurrentServiceObjectiveName | |
$CurrentEdition = $Database.Edition | |
# If the database edition is Basic or Free then skip the database, also if the sizing is | |
# already at the scaled down tier then skip | |
if ($CurrentEdition -eq "Basic" -or $CurrentEdition -eq "Free") { continue } | |
if ($CurrentPricingTier -eq "S0" -or $CurrentPricingTier -eq "S1" -or $CurrentPricingTier -eq "DW100") { continue } | |
# Capture the existing scale information in the database tag information | |
$Tags = $Database.Tags | |
if ($Tags -eq $null) { $Tags = @{} } | |
$Tags["previousServiceObjectiveName"] = $CurrentPricingTier | |
$Tags["previousEdition"] = $CurrentEdition | |
# Determine the new pricing tier based on the edition | |
$NewPricingTier = "S1" | |
if ($Database.Edition -eq "DataWarehouse") { $NewPricingTier = "DW100" } | |
# Set the new edition information | |
$NewEdition = $Database.Edition | |
if ($Database.Edition -eq "Premium") { $NewEdition = "Standard" } | |
Write-Output "Changing scale of database $($Database.ResourceId)" | |
# Scale the database | |
$Database | Set-AzureRmSqlDatabase -Edition $NewEdition -RequestedServiceObjectiveName $NewPricingTier -Tags $Tags | |
} | |
} | |
} | |
# Scales down app services to the lowest possible standard pricing plan | |
function ScaleAppServicePlans { | |
Param ( | |
$ResourceGroup | |
) | |
# Get the app service plans for the resource group | |
$AppServicePlans = Get-AzureRmAppServicePlan -ResourceGroupName $ResourceGroup.ResourceGroupName | |
foreach ($AppServicePlan in $AppServicePlans) { | |
# Check to see if the service plan has been tagged to maintain the existing Sku | |
if ($AppServicePlan.Tags.Keys -contains "keepScale" -and $AppServicePlan.Tags["keepScale"] -eq $true) { | |
Write-Output "Application service plan $($AppServicePlan.Id) has been flagged to maintain the existing Sku" | |
continue | |
} | |
# If the service plan is not standard or premium then ignore it | |
if ($AppServicePlan.Sku.Tier -ne "Standard" -and $AppServicePlan.Sku.Tier -ne "Premium") { continue } | |
# Record the current SKU information in the app service plans tags | |
$Tags = $AppServicePlan.Tags | |
if ($Tags -eq $null) { $Tags = @{} } | |
$Tags["previousSkuName"] = $AppServicePlan.Sku.Name | |
Write-Output "Changing scale of application service plan $($AppServicePlan.Id)" | |
# Scale the service plan | |
Set-AzureRmAppServicePlan -ResourceGroupName $AppServicePlan.ResourceGroup -Name $AppServicePlan.Name -Tier Standard -WorkerSize Small | |
# Set the tags for the resource | |
Set-AzureRmResource -ResourceId $AppServicePlan.Id -Tag $Tags -Force | |
} | |
} | |
# Process each resource group | |
foreach ($rg in Get-AzureRmResourceGroup) { | |
# Don't scale a resource group if it has been flagged as a "keep alive" environment | |
if ($rg.Tags.Keys -contains "keepAlive" -and $rg.Tags["keepAlive"] -eq $true) { | |
Write-Output "Resource group $($rg.ResourceGroupName) has been flagged as an environment to keep alive" | |
continue | |
} | |
ScaleDatabases -ResourceGroup $rg | |
ScaleAppServicePlans -ResourceGroup $rg | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment