Last active
January 5, 2017 16:14
-
-
Save dazfuller/92f584a26bbb109c05cd48a0d9d1304d 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
workflow TestWorkflowRunbook | |
{ | |
$connectionName = "AzureRunAsConnection" | |
try | |
{ | |
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName | |
"Logging into 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 | |
} | |
} | |
# Get the resource groups which have not been flagged to keep alive | |
$resourceGroups = Get-AzureRmResourceGroup | Where-Object -FilterScript { -not ( $_.Tags.Keys -contains "keepAlive" -and $_.Tags["keepAlive"] -eq $true ) -and $_.ResourceGroupName -like "df*" } | |
ForEach -Parallel ($rg in $resourceGroups) | |
{ | |
Parallel | |
{ | |
# Scale Databases | |
Sequence | |
{ | |
$SqlServers = Get-AzureRmSqlServer -ResourceGroupName $rg.ResourceGroupName | |
ForEach ($sqlServer in $sqlServers) | |
{ | |
ForEach -Parallel ($db in $sqlServer | Get-AzureRmSqlDatabase | Where-Object -FilterScript { -not ( $_.Tags.Keys -contains "keepScale" -and $_.Tags["keepScale"] -eq $true ) }) | |
{ | |
# Capture the current pricing tier and edition | |
$currentPT = $db.CurrentServiceObjectiveName | |
$currentEdition = $db.Edition | |
if ( ( $currentEdition -eq "Basic" -or $currentEdition -eq "Free" ) -or ( $currentPT -eq "S0" -or $currentPT -eq "S1" -or $currentPT -eq "DW100" ) ) | |
{ | |
# Skip if the database is already at an acceptable pricing tier or edition | |
Write-Output "Valid size $($db.ResourceId)" | |
} | |
else | |
{ | |
# Set the tags for the database | |
$tags = @{} | |
$tags += @{ 'previousServiceObjectiveName' = $currentPT } | |
$tags += @{ 'previousEdition' = $currentEdition } | |
if ($db.Tags -ne $null) | |
{ | |
$keys = $db.Tags.Keys | Where-Object -FilterScript { $_ -ne "previousServiceObjectiveName" -and $_ -ne "previousEdition" } | |
ForEach ($key in $keys) | |
{ | |
$tags += @{ $key = $db.Tags[$key] } | |
} | |
} | |
# Determine the new pricing tier based on the edition | |
$newPricingTier = "S1" | |
if ($db.Edition -eq "DataWarehouse") { $newPricingTier = "DW100" } | |
# Set the new edition information | |
$newEdition = $db.Edition | |
if ($db.Edition -eq "Premium") { $newEdition = "Standard" } | |
# Scale the database | |
Write-Output "Changing scale of database $($db.ResourceId)" | |
Set-AzureRmSqlDatabase ` | |
-ResourceGroupName $db.ResourceGroupName ` | |
-ServerName $db.ServerName ` | |
-DatabaseName $db.DatabaseName ` | |
-Edition $newEdition ` | |
-RequestedServiceObjectiveName $newPricingTier ` | |
-Tags $tags | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment