-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSet-AzureSqlDatabaseEdition.ps1
94 lines (73 loc) · 3.63 KB
/
Set-AzureSqlDatabaseEdition.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<#
.SYNOPSIS
Vertically scale (up or down) an Azure SQL Database
.DESCRIPTION
This runbook enables one to vertically scale (up or down) an Azure SQL Database using Azure Automation.
There are many scenarios in which the performance needs of a database follow a known schedule.
Using the provided runbook, one could automatically schedule a database to a scale-up to a Premium/P1
database during peak hours (e.g., 7am to 6pm) and then scale-down the database to a Standard/S0 during
non peak hours (e.g., 6pm-7am).
.PARAMETER SqlServerName
Name of the Azure SQL Database server (Ex: bzb98er9bp)
.PARAMETER DatabaseName
Target Azure SQL Database name
.PARAMETER Edition
Desired Azure SQL Database edition {Basic, Standard, Premium}
For more information on Editions/Performance levels, please
see: http://msdn.microsoft.com/en-us/library/azure/dn741336.aspx
.PARAMETER PerfLevel
Desired performance level {Basic, S0, S1, S2, P1, P2, P3}
.PARAMETER Credential
Credentials for $SqlServerName stored as an Azure Automation credential asset
When using in the Azure Automation UI, please enter the name of the
credential asset for the "Credential" parameter
.EXAMPLE
Set-AzureSqlDatabaseEdition
-SqlServerName bzb98er9bp
-DatabaseName myDatabase
-Edition Premium
-PerfLevel P1
-Credential myCredential
.NOTES
Author: Joseph Idziorek
Last Updated: 11/22/2014
#>
workflow Set-AzureSqlDatabaseEdition
{
param
(
# Name of the Azure SQL Database server (Ex: bzb98er9bp)
[parameter(Mandatory=$true)]
[string] $SqlServerName,
# Target Azure SQL Database name
[parameter(Mandatory=$true)]
[string] $DatabaseName,
# Desired Azure SQL Database edition {Basic, Standard, Premium}
[parameter(Mandatory=$true)]
[string] $Edition,
# Desired performance level {Basic, S0, S1, S2, P1, P2, P3}
[parameter(Mandatory=$true)]
[string] $PerfLevel,
# Credentials for $SqlServerName stored as an Azure Automation credential asset
# When using in the Azure Automation UI, please enter the name of the credential asset for the "Credential" parameter
[parameter(Mandatory=$true)]
[PSCredential] $Credential
)
inlinescript
{
Write-Output "Begin vertical scaling script..."
# Establish credentials for Azure SQL Database server
$Servercredential = new-object System.Management.Automation.PSCredential($Using:Credential.UserName, (($Using:Credential).GetNetworkCredential().Password | ConvertTo-SecureString -asPlainText -Force))
# Create connection context for Azure SQL Database server
$CTX = New-AzureSqlDatabaseServerContext -ManageUrl “https://$Using:SqlServerName.database.windows.net” -Credential $ServerCredential
# Get Azure SQL Database context
$Db = Get-AzureSqlDatabase $CTX –DatabaseName $Using:DatabaseName
# Specify the specific performance level for the target $DatabaseName
$ServiceObjective = Get-AzureSqlDatabaseServiceObjective $CTX -ServiceObjectiveName "$Using:PerfLevel"
# Set the new edition/performance level
Set-AzureSqlDatabase $CTX –Database $Db –ServiceObjective $ServiceObjective –Edition $Using:Edition -Force
# Output final status message
Write-Output "Scaled the performance level of $Using:DatabaseName to $Using:Edition - $Using:PerfLevel"
Write-Output "Completed vertical scale"
}
}