-
Notifications
You must be signed in to change notification settings - Fork 0
/
VMScheduledScaler.ps1
187 lines (123 loc) · 6.54 KB
/
VMScheduledScaler.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<#
.SYNOPSIS
Through this Automation script you can schedule a specific Resource Group Azure VM at specific times for resizing.
It is suggested that you run 2 schedules, for example:
At night hours, downscale to the lowest VM size for cost reasons.
At the start of office hours, upscale to the usual size for productivity.
This script is highly based on Kay Singh's script for Vertical Scaling, I added scheduling variables for practical use.
Check his Runbook here: https://docs.microsoft.com/en-us/azure/virtual-machines/virtual-machines-linux-vertical-scaling-automation
Note: Downtime of +- 4 minutes applies when the job starts.
.DESCRIPTION
This script will adjust the size of a Microsoft Azure virtual machine based on input given into the new schedule.
When making a Schedule or doing a test run you are required to declare:
* The desired to be VM Size
* The Resource Group name of the VM that is to be changed
* The name of the VM that is to be changed
Below in the script at lines 120 and 122 you are required to declare:
* A Credentials Asset
* A Variable Asset with the Subscription ID in it
This script is based on the runbook by Kay Singh, 75% of credit goes to him.
https://gallery.technet.microsoft.com/scriptcenter/Vertically-scale-up-an-d6c15c54
.PARAMETER VirtualMachineName
String name of the VM that will be changed in size.
.PARAMETER VMSize
String name of the desired size the specified VM will be receiving.
Allowed 'VMSize'values:
DS:
Standard_DS1,Standard_DS2,Standard_DS3,Standard_DS4,Standard_DS11,Standard_DS12,Standard_DS13,Standa
rd_DS14,Standard_DS1_v2,Standard_DS
2_v2,Standard_DS3_v2,Standard_DS4_v2,Standard_DS5_v2,Standard_DS11_v2,Standard_DS12_v2,Standard_DS13_v2,Standard_DS14_v2
,Standard_DS15_v2
A:
Standard_A0,Standard_A1,Standard_A2,Standard_A3,Standard_A5,Standard_A4,Standard_A6,Standard_A7,Basic_A0,Basic_A
1,Basic_A2,Basic_A3,Basic_A4, Standard_A1_v2,Standard_A2m_v2,Standard_A2_v2,Standard_A4m_v2,Standard_A4_v2,Standard_A8m_v2,Standard_A8_v2,
Standard_A8,Standard_A9,Standard_A10,Standard_A11
D:
Standard_D1_v2,Standard_D2_v2,Standard_D3_v2,Standard_D4_v2,Standard_D5_v2,Standard_D11_v2,
Standard_D12_v2,Standard_D13_v2,Standard_D14_v2,Standard_D15_v2,Standard_D1,Standard_D2,Standard_D3,Standard_D4,Standard_D11,Standard_D12,Standard_D13,Standard_D14
F:
Standard_F1,Standard_F2,Standard_F4,Standard_F8,Standard_F16,Standard_F1s,Standard_F2s,Standard_F4s,Standard_F8s,Standard_F16s
H:
Standard_H8,Standard_H16,Standard_H8m,Standard_H16m,Standard_H16r,Standard_H16mr
G:
Standard_G1,Standard_G2,Standard_G3,Standard_G4,Standard_G5,
GS:
Standard_GS1,Standard_GS2,Standard_GS3,Standard_GS4,Standard_GS5
NV:
Standard_NV6,Standard_NV12,Standard_NV24
NOTE: It is likely that not all of these options apply to your specific VM, follow the guideline below to check the sizes that apply to your VM.
> To see the possible sizes for your specific VM check the following: Click the specified VM in the dashboard -> Virtual Machine Overview -> Size and note what is available
.PARAMETER ResourceGroup
String that contains the name of Resource Group where your specified VM is a part of.
.NOTES
Author: Siebert Timmermans
Last Updated: 27/03/2017
Version 1.1 - Updated User Experience and logging.
DO NOT FORGET THE CREDENTIAL/KEY DECLARATION IN THE SCRIPT BELOW
#>
workflow VMScheduledScaler
{
Param
(
[parameter(Mandatory=$true)]
[String] $VirtualMachineName,
[parameter(Mandatory=$true)]
[string] $VMSize,
[parameter(Mandatory=$true)]
[string] $ResourceGroup
)
InlineScript {
# Introduction Output part 1
$VERSION = "1.1"
$currentTime = (Get-Date).AddHours(2)#.ToUniversalTime()
Write-Output "Runbook started. Version: $VERSION"
Write-Output "Schedule received for current time in CET [$($currentTime.ToString("dddd dd MMM yyyy HH:mm:ss"))]. Running job..."
# Credentials and Subscription ID declaration, DO NOT FORGET TO FILL IN THESE!
$Cred = Get-AutomationPSCredential -Name 'siebertsCredentials'
$null = Add-AzureRmAccount -Credential $Cred -ErrorAction Stop
$SubId = Get-AutomationVariable -Name 'AzureKey'
$null = Set-AzureRmContext -SubscriptionId $SubId -ErrorAction Stop
# Introduction Output part 2
if($SubId.length -gt 0)
{
Write-Output "Specified subscription ID: [$SubID]"
}
else
{
throw "No subscription name was specified. Create a subscription Variable Asset and add it to the Runbook in the declaration variables manually."
}
if($Cred -ne $null)
{
Write-Output "Attempting to authenticate as: [$($Cred.UserName)]"
}
else
{
throw "No automation credential name was specified. Create a Credential Asset and add it to the Runbook in the declaration variables manually."
}
Write-Output "Specified VM name: [$Using:VirtualMachineName]"
Write-Output "Specified VM Resource Group: [$Using:ResourceGroup]"
Write-Output "Desired VM size: [$Using:VMSize]"
Write-Output "`n----------------------------------------------------------------------"
# Check if specified VM can be found
try {
$vm = Get-AzureRmVm -ResourceGroupName $Using:ResourceGroup -VMName $Using:VirtualMachineName -ErrorAction Stop
} catch {
Write-Error "Virtual Machine not found"
exit
}
# Output current VM Size
$currentVMSize = $vm.HardwareProfile.vmSize
Write-Output "`nFound the specified Virtual Machine: $Using:VirtualMachineName"
Write-Output "Current size: $currentVMSize"
# Change to new VM Size and report
$newVMSize = $Using:VMSize
Write-Output "`nNew size will be: $newVMSize"
Write-Output "`n----------------------------------------------------------------------"
$vm.HardwareProfile.VmSize = $newVMSize
Update-AzureRmVm -VM $vm -ResourceGroupName $Using:ResourceGroup
$updatedVm = Get-AzureRmVm -ResourceGroupName $Using:ResourceGroup -VMName $Using:VirtualMachineName
$updatedVMSize = $updatedVm.HardwareProfile.vmSize
Write-Output "`n----------------------------------------------------------------------"
Write-Output "`nSize updated to: $updatedVMSize"
}
}