-
Notifications
You must be signed in to change notification settings - Fork 0
/
RDSH-Scale.ps1
428 lines (302 loc) · 15 KB
/
RDSH-Scale.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# Author: Michael Troisi
# Date: 8/5/2019
#
# This script calculates the utlization of each session host and determinies if
# more session hosts need to be started or stopped based on a breadth algorithm
# NOTE: This script operates on the assumption.
# That the VM name in Azure is the hostname of the machine.
# Configuration Variables
# ----------------------------------------------------------------------------------
# This setting prevents any configuration changes to be made when set to 1
$TESTING_MODE = 0
# path to store log files. Trailing backslash necessary
$LogPath = ''
$LogFile = Get-Date -Format "yyyyMMdd"
$ConnectionBroker = ''
$CollectionName = ''
$TagValues = ''
# Maximum percentage utilization
$MaxUtilization = 80
# Minimum percentage utlization
$MinUtilization = 40
# Minimum session hosts to remain online
$MinSessionHost = 1
# Shutdown delay in seconds
$ShutdownTimer = '300'
$MessageTitle = 'System Scaling Underway'
$MessageBody = 'Please save your work and logoff!'
# ----------------------------------------------------------------------------------
# Declare functions
# ----------------------------------------------------------------------------------
# logging function
Function Write-Log {
param ( [String]$Message )
$DatePrefix = Get-Date -Format "MM/dd/yyyy HH:mm:ss : "
Write-Host $Message
Out-File -FilePath ($LogPath + $LogFile + ".log") -Append -InputObject ( $DatePrefix + $Message )
}
# Set the power state of an Azure VM. Input is session host object and the variable, on or off.
Function Set-AzureVMPower {
param ( $SessionHost,
[String]$PowerState )
Write-Host ( $SessionHost )
# block attempt to turn off last session host
if ( ($SessionHostTable | Where-Object PowerOn).Count -le $MinSessionHost -and
$PowerState -like 'off' ) {
Write-Log ( "Set-AzureVMPower off was called on the last running session host. No action taken" )
}
elseif ($PowerState -like 'on' -and $SessionHost.PowerOn -eq 0) {
if ( -not $TESTING_MODE ) {
Write-Log ( "Starting VM: " + $SessionHost.VMName )
# Try to start VM
try {
Start-AzureRmVM -Name $SessionHost.VMName -ResourceGroupName $ResourceGroupName -ErrorAction Stop
}
catch {
Write-Log ( "An error has occurred when starting VM: " + $SessionHost.VMName )
}
else {
Write-Log ("TESTING: Starting VM: " + $SessionHost.VMName)
}
# update session host table
$SessionHost.PowerOn = 1 | Out-Null
}
}
elseif ($PowerState -like 'off' -and $SessionHost.PowerOn -eq 1) {
if ( -not $TESTING_MODE ) {
Write-Log ( "Stopping VM: " + $SessionHost.VMName )
# Try to stop VM
try {
Stop-AzureRmVM -Name $SessionHost.VMName -ResourceGroupName $ResourceGroupName -Force
}
catch {
Write-Log ( "An error has occurred when stopping VM: " + $SessionHost.VMName )
}
else {
Write-Log ("TESTING: Stopping VM: " + $SessionHost.VMName)
}
# update session host table
$SessionHost.PowerOn = 0 | Out-Null
}
}
else {
Write-Log ( "Unknown power state: " + $Value + ". No action was taken on VM: " + $SessionHost.VMName )
}
}
# Send a message to all connected users on a session host. Input is session host object.
Function Send-UserMessage {
param ( $SessionHost )
# Get user session list for server being scaled down
$SessionHostSessionList = $UserSessionList | Where-Object HostServer -Like $SessionHost.FQDN
# Get count of user sessions for server being scaled down
$SessionHostUserCount = $SessionHostSessionList.Count
# Cancel function early if no users on host
if ( $SessionHostUserCount -le 0 ) {
Write-Log ( "No users on " + $SessionHost.FQDN )
return 0
}
$SessionHostSessionList | ForEach-Object {
try {
if ( -not $TESTING_MODE ) {
Write-Log ("Sending RDUsermessage. SessionHostName: " + $SessionHost.FQDN + " UserName: " + $_.UserName)
Send-RDUserMessage -HostServer $SessionHost.FQDN -UnifiedSessionID $_.UnifiedSessionId -MessageTitle $MessageTitle -MessageBody $MessageBody
}
else {
Write-Log ("TESTING: Sending RDUsermessage. SessionHostName: " + $SessionHost.FQDN + " UserName: " + $_.UserName)
}
}
catch {
Write-Log ( "An error has occurred when sending an RDUserMessage. SessionHostName: " + $SessionHost.FQDN + " UserName: " + $_.UserName )
}
}
# delay shutdown for users to sign off
Write-Log ( "Waiting " + $ShutdownTimer + " seconds before shutdown" )
if ( -not $TESTING_MODE ) {
Start-Sleep $ShutdownTimer
}
}
# Begins the scale down operation on a session host. Input is a session host collection.
Function Start-ScaleDown {
if ( $OnlineSessionHostCount -le $MinSessionHost ) {
Write-Log "Session hosts at minumum. Will not scale down"
return
}
# finds the host with the least sessions
$SessionHost = $SessionHostTable | Where-Object PowerOn |
Sort-Object -Property CurrentSessions -Descending | Select-Object -Last 1
if ( Test-ScaleDown $SessionHost ) {
Write-Log ("Beginning scale down on " + $SessionHost.VMName )
# disallow new connections
Set-NewConnectionAllowed -SessionHost $SessionHost -Value No
# notify users of shutdown and wait
Send-UserMessage $SessionHost
# shutdown session host
Set-AzureVMPower -SessionHost $SessionHost -PowerState off
}
else {
Write-Log ( "Test-ScaleDown returns 0. Scale down not committed on " + $SessionHost.VMName )
}
Write-Log "Scale down completed"
}
# Begins the scale up operation on a session host.
Function Start-ScaleUp {
if ( $OnlineSessionHostCount -ge $CollectionConfiguration.Count ) {
Write-Log "Session hosts at maximum. Will not scale up."
return
}
# picks offline session host with highest relative weight
$NextSessionHost = $SessionHostTable | Where-Object PowerOn -EQ 0 |
Sort-Object -Property RelativeWeight -Descending |Select-Object -First 1
Write-Log ( "Beginning scale up on " + $NextSessionHost.VMName )
# turns on the first session host in the queue
Set-AzureVMPower -SessionHost $NextSessionHost -PowerState 'On'
# enables new connections to the session host
Set-NewConnectionAllowed -SessionHost $NextSessionHost -Value Yes
}
# Estimate utlization after a scale down. Input is a session host object to be scaled down.
# returns 1 if scale down is viable, 0 if not.
Function Test-ScaleDown {
param ( $SessionHost )
$CurrentRatio = ($UserSessionList.Count.ToString() + "/" +
($OnlineSessionHosts.SessionLimit | Measure-Object -Sum).Sum).ToString()
Write-Log ( "Test-ScaleDown Current farm utilization is " +
(Get-FarmUtilization $OnlineSessionHosts) + "% : (" + $CurrentRatio + ")")
# run calculations for environment where this session host is scaled down
$NewSessionHostTable = $OnlineSessionHosts | Where-Object FQDN -NotContains $SessionHost.FQDN
$NewFarmUtilization = Get-FarmUtilization $NewSessionHostTable
$NewRatio = ($UserSessionList.Count.ToString() + "/" +
(($NewSessionHostTable | Where-Object PowerOn).SessionLimit | Measure-Object -Sum).Sum).ToString()
Write-Log ( "Estimated new farm utlization is " + $NewFarmUtilization + "% : (" + $NewRatio + ")")
if ($NewFarmUtilization -ge $MaxUtilization) {
Write-Log "Scale down operation is not viable"
return 0
}
else {
Write-Log "Scale down operation is viable"
return 1
}
}
# Get average utilization of session hosts in a collection. Input is collection configuration
# Returns average utilization of session hosts
Function Get-FarmUtilization {
param ( [Array]$ServerCollection )
$UserCount = $UserSessionList.Count
$TotalSeats = ($ServerCollection.SessionLimit | Measure-Object -Sum).Sum
return ( [Math]::Round( (($UserCount / $TotalSeats) * 100 | Measure-Object -Average).Average))
}
# Sets session host connection allowed setting, Input is a session host object and yes or no.
Function Set-NewConnectionAllowed {
param ( $SessionHost,
[String]$Value )
try {
if ( -not $TESTING_MODE ) {
Write-Log ( "Setting new connection allowed on " + $SessionHost.FQDN + " to " + $Value )
Set-RDSessionHost -SessionHost $SessionHost.FQDN -NewConnectionAllowed $Value -ConnectionBroker $ConnectionBroker
}
else {
Write-Log ( "TESTING: setting new connection allowed on " + $SessionHost.FQDN + " to " + $Value )
}
# update session host table
$SessionHost.NewConnectionsAllowed = if ($Value -like "yes") {1} else {0}
}
catch {
Write-Log ( "Error while setting NewConnectionAllowed setting for " + $SessionHost.FQDN )
}
}
# ----------------------------------------------------------------------------------
# Gather environment information
# ----------------------------------------------------------------------------------
#create delimiter between script runs
Write-Log "-------------------------------------------------------------------------"
Write-Log "Getting Collection Configuration..."
$CollectionConfiguration = Get-RDSessionCollectionConfiguration -CollectionName $CollectionName -ConnectionBroker $ConnectionBroker -LoadBalancing |
Sort-Object -Property RelativeWeight -Descending
Write-Log "Getting Session Host Settings..."
$RDSessionHost = Get-RDSessionHost -CollectionName $CollectionName -ConnectionBroker $ConnectionBroker
Write-Log "Getting list of user sessions..."
$UserSessionList = Get-RDuserSession -CollectionName $CollectionName -ConnectionBroker $ConnectionBroker
Write-Log "Getting status of Azure virtual machines..."
$AzureVMList = Get-AzureRmVM -status | Where-Object {$_.Tags.Values -like $TagValues}
# Gets the resource group name from a session host.
$ResourceGroupName = $AzureVMList.ResourceGroupName[0]
# ----------------------------------------------------------------------------------
# Check session hosts and gather information
# ----------------------------------------------------------------------------------
# Create table of session hosts
try { $SessionHostTable.Clear() } catch {}
$SessionHostTable = New-Object System.Data.DataTable
$SessionHostTable.Columns.Add("FQDN", "string") | Out-Null
$SessionHostTable.Columns.Add("VMName", "string") | Out-Null
$SessionHostTable.Columns.Add("RelativeWeight", "int") | Out-Null
$SessionHostTable.Columns.Add("CurrentSessions", "int") | Out-Null
$SessionHostTable.Columns.Add("SessionLimit", "int") | Out-Null
$SessionHostTable.Columns.Add("Utilization", "int") | Out-Null
$SessionHostTable.Columns.Add("NewConnectionsAllowed", "int") | Out-Null
$SessionHostTable.Columns.Add("PowerOn", "int") | Out-Null
Write-Log "##"
ForEach ($SessionHost in $CollectionConfiguration) {
$row = $SessionHostTable.NewRow()
# populate table with data
$row.FQDN = $SessionHost.SessionHost
$row.VMName = $SessionHost.SessionHost.Split('.')[0]
$row.RelativeWeight = $SessionHost.RelativeWeight
$row.CurrentSessions = ($UserSessionList | Where-Object HostServer -Like $row.FQDN).Count
$row.SessionLimit = $SessionHost.SessionLimit
$row.Utilization = [Math]::Round( ( $row.CurrentSessions / $row.SessionLimit) * 100 )
$row.NewConnectionsAllowed = if ( ($RDSessionHost | Where-Object SessionHost -Like $row.FQDN).NewConnectionAllowed -like "yes" ) {1} else {0}
$row.PowerOn = if ( ($AzureVMList | Where-Object Name -Like $row.VMName).PowerState -Like '*running*') {1} else {0}
$SessionHostTable.Rows.Add($row)
# Check and correct any discrepencies
# Check if session host is empty, not allowing new ones, and powered on
if ( $row.CurrentSessions -lt 1 -and $row.NewConnectionsAllowed -eq 0 -and $row.PowerOn -eq 1 ) {
Write-Log ( $row.VMName + " has no sessions, is not allowing new ones, yet powered on. Turning off now..." )
Set-AzureVMPower -SessionHost $row -PowerState off
}
}
# ----------------------------------------------------------------------------------
# Check online hosts and display statistics
# ----------------------------------------------------------------------------------
$OnlineSessionHosts = $SessionHostTable | Where-Object PowerOn
$OnlineSessionHostCount = ( $OnlineSessionHosts | Measure-Object ).Count
$AboveThresholdCount = 0
$BelowThresholdCount = 0
ForEach ($SessionHost in $OnlineSessionHosts ) {
$Threshold = " "
if ( $SessionHost.Utilization -ge $MaxUtilization ) {
$Threshold = "+"
$AboveThresholdCount = $AboveThresholdCount + 1
}
elseif ( $SessionHost.Utilization -le $MinUtilization ) {
$Threshold = "-"
$BelowThresholdCount = $BelowThresholdCount + 1
}
Write-Log ( $SessionHost.VMName + " Utlization is at " + $SessionHost.Utilization + "%" +
" : (" + $SessionHost.CurrentSessions + "/" + $SessionHost.SessionLimit + ") " + $Threshold )
}
Write-Log "##"
Write-Log ( "Current farm utilization is at " + (Get-FarmUtilization $OnlineSessionHosts) + "%" +
" : (" + $UserSessionList.Count + "/" + ( $OnlineSessionHosts.SessionLimit | Measure-Object -Sum).Sum + ")" )
Write-Log ( $OnlineSessionHostCount.ToString() + " Session host(s) online" )
Write-Log ( "Total session hosts above threshold: " + $AboveThresholdCount )
Write-Log ( "Total session hosts below threshold: " + $BelowThresholdCount )
# ----------------------------------------------------------------------------------
# Scale up/down
# ----------------------------------------------------------------------------------
# if online session hosts below minumum set to be online. Shoudn't need this
if ( $OnlineSessionHostCount -lt $MinSessionHost ) {
Write-Log "WARNING: Online session hosts have fallen below the minimum"
Start-ScaleUp
}
# all session hosts above max threshold
elseif ( $AboveThresholdCount -ge $OnlineSessionHostCount ) {
Write-Log "##"
Start-ScaleUp
}
# farm utlilization below minimum or more session hosts below
# threshold than are above threshold
elseif( (Get-FarmUtilization $OnlineSessionHosts) -le $MinUtilization -or
$BelowThresholdCount -gt $AboveThresholdCount) {
Write-Log "##"
Start-ScaleDown
}
Write-Log "DONE"