Skip to content

Instantly share code, notes, and snippets.

@dazfuller
Last active January 5, 2017 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dazfuller/92f584a26bbb109c05cd48a0d9d1304d to your computer and use it in GitHub Desktop.
Save dazfuller/92f584a26bbb109c05cd48a0d9d1304d to your computer and use it in GitHub Desktop.
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