-
Notifications
You must be signed in to change notification settings - Fork 1
/
Citrix-IntelliScale.ps1
458 lines (417 loc) · 23.6 KB
/
Citrix-IntelliScale.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#requires -version 3.0
<#
This script can be used to intelligently power up/down machines on prem or in the cloud (AWS,Azure,GCP) based upon schedules or 'load'.
Steve Noel
7-10-2018
Modification history:
(Date and Change Description)
#>
<#
.SYNOPSIS
This script can be used to intelligently power up/down machines on prem or in the cloud (AWS,Azure,GCP) based upon schedules or 'load'.
.DESCRIPTION
.PARAMETER (parameter1)
(Enter details on how paraemter works/requirements)
.PARAMETER (parameter2)
(Enter details on how paraemter works/requirements)
Azure:
Install-Module -Name azurerm
Login-AzureRmAccount
<Needed? Enable-AzureRmContextAutosave,Connect-AzureRmAccount (sign in)>
.EXAMPLE
& '.\(Name of Script).ps1' -DeliveryController Citrix-ddc1 -LogDir C:\temp -ScheduleMode -SchedStart 6 -SchedFinish 16 -DGName DG-Server2012R2 -BaseTag Base -OnPrem -LogOnly
This script will look at the dlivery group 'DG-Server2012R2' on the Delivery Controller 'Citrix-ddc1'. It's using the mode 'ScheduleMode' which powers up/down machines based upon Times set
in 'SchedStart' and 'SchedFinish' (military time). At 'SchedStart', it makes sure machines are powered on and available for connections all the way up to the 'SchedFinish' time. The 'BaseTag' Base is
used to denote that these machines won't get controlled in this script. These are your base machines that you don't want power managed. The 'OnPrem' paraemter is notifying the script to
use certain commands geared for on premesis deployments. Lastly we are using the 'LogOnly' parameter to put the script in an audit mode. So it will report everything, but won't actually execute the action.
.EXAMPLE
& '.\(Name of Script).ps1' -DeliveryController Citrix-ddc1 -LogDir C:\temp -ScheduleMode -SchedStart 6 -SchedFinish 16 -DGName DG-Server2012R2 -BaseTag Base -IgnoreTag UnIntelli -AWS -AWSProfile My-AWS-Credentials -SMTPserver smtp.domain.local -ToAddress Steve@asdf.com -FromAddress Steve@asdf.com -Email
This script performs like above but is geared for AWS deployments, with the -AWS parameter. It also ignores any VDAs tagged with 'UnIntelli' tag. It uses the AWS Credential Profile 'AWSProfile' called My-AWS-Credentials. Lastly it emails the results to the defined email addresses.
.NOTES
(Enter specific things to note for this script)
#>
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$True,Position=1)][string]$DeliveryController,
[Parameter(Mandatory=$True)][string]$LogDir,
[Switch]$ScheduleMode,
[Switch]$LoadMode,
[int]$SchedStart,
[int]$SchedFinish,
[int]$ThreshHoldLoad, #new
[int]$CapacityBuffer, #new
[int]$MinNumberAvail, #new
[int]$ThresholdPowerOff, #new
[Parameter(Mandatory=$True)][string]$DGName,
[string]$BaseTag,
[string]$IgnoreTag,
#[ValidateSet($True,$False)]
[Switch]$AWS,
[String]$AWSProfile,
[Switch]$OnPrem,
[Switch]$Azure,
[Switch]$Weekend,
[Switch]$LogOnly,
[Switch]$Email,
[String]$SMTPserver,
[string]$ToAddress,
[string]$FromAddress
)
Clear-Host
#Import-Module AWSPowerShell
Add-PSSnapIn citrix*
##### Defines log path ####
$firstcomp = Get-Date
$filename = $firstcomp.month.ToString() + "-" + $firstcomp.day.ToString() + "-" + $firstcomp.year.ToString() + "-" + $firstcomp.hour.ToString() + "-" + $firstcomp.minute.ToString() + ".txt"
$outputloc = $LogDir + "\" + $filename
##### Defines log path ####
$hostname = hostname
Start-Transcript -Path $outputloc
############ Function Schedule Based ###########
Function Get-IntelliMode
{
If ($ScheduleMode -or $LoadMode)
{
$CurrentHour = get-date -format HH
Try
{
$machines = Get-BrokerDesktop -AdminAddress $DeliveryController -MaxRecordCount 10000 -DesktopGroupName $DGName
$DeliveryGroup = Get-BrokerDesktopGroup -AdminAddress $DeliveryController -Name $DGName
}
Catch
{
Write-host 'Unable to run command Get-BrokerDesktop, check $DeliveryController and $DGName variables'
exit
}
if ($Weekend) {$IsWeekend = (get-date).DayOfWeek | Where-Object {$_ -like 'Saturday' -or $_ -like 'Sunday'}}
if ($ScheduleMode)
{
if ($SchedStart -le $CurrentHour -and $SchedFinish -ge $CurrentHour -and $IsWeekend -eq $null)
{
#write-host "In Business"
Enter-ScheduleIn($machines)
}
Else
{
#Write-host "Outside Business Hours"
Enter-ScheduleOut($machines)
}
}
if ($LoadMode)
{
If ($DeliveryGroup.SessionSupport -like 'Multi*')
{
$machinesLoad = $machines | Where-Object {$_.Tags -notcontains $IgnoreTag -and $_.Tags -notcontains $BaseTag}
$totalload = [math]::Round(($machinesLoad.loadindex | Measure-Object -Sum).Sum / ($machinesLoad.InMaintenanceMode | Group-Object {$_.Name -eq 'False'} | Select-Object -ExpandProperty Count)/100)
if ($totalload -ilt $threshhold)
{
Enter-LoadModeRemoveCapacity($totalload,$machinesLoad)
}
Else
{
Enter-LoadModeAddCapacity($totalload,$machinesLoad)
}
}
If ($DeliveryGroup.SessionSupport -like 'Single*')
{
#Use $MinNumberAvail (default 2) and $CapacityBuffer (default 10%)
#$machinesLoad = $machines | Where-Object {$_.Tags -notcontains $IgnoreTag -and $_.Tags -notcontains $BaseTag}
#$totalload = [math]::Round($machinesLoad.AssociatedUserNames.count / ($machinesLoad.InMaintenanceMode | Group-Object {$_.Name -eq 'False'} | Select-Object -ExpandProperty Count)*100)
}
}
}
Else
{
Write-host "Please choose a mode paraemter: -ScheduleMode or -LoadMode"
}
}
############ End Function Schedule Based ###########
Function Enter-ScheduleIn
{
Write-host "******************************************"
Write-host "Bussiness Hours"
$machinesIn = $machines | Where-Object {$_.Tags -notcontains $IgnoreTag -and $_.Tags -notcontains $BaseTag}
if ($machinesIn)
{
Write-Host "Working on Machines:"
$machinesIn | Format-Table MachineName,DesktopGroupName,LoadIndex,RegistrationState,InMaintenanceMode,Tags
Foreach ($machineIn in $machinesIn)
{
#Maintenance Mode
Write-host $machineIn.DNSName.Split(".",2)[0] "(Setting Maintenance Mode - FALSE)"
if (!($LogOnly))
{
Try
{
$machineIn | Set-BrokerMachine -InMaintenanceMode $False | Out-Null
}
Catch
{
Write-host $machineIn.DNSName.Split(".",2)[0] "(Unable to put machine in Maintenance Mode)"
}
}
#Power On machines
if ($OnPrem)
{
Write-host "OnPrem"
If ($machineIn.powerstate -eq 'Off')
{
Write-Host $machineIn.DNSName.Split(".",2)[0] "(Starting Machine)"
if (!($LogOnly))
{
Try
{
New-BrokerHostingPowerAction -AdminAddress $DeliveryController -Action TurnOn -MachineName $machineIn.HostedMachineName | Out-Null
}
Catch
{
Write-Host $machineIn.DNSName.Split(".",2)[0] "(Unable to Start OnPrem Instance)"
}
}
}
Else
{
Write-host $machineIn.DNSName.Split(".",2)[0] "(Already Powered On)"
}
}
if ($AWS)
{
Write-host "AWS"
Try
{
$instance = (Get-EC2Instance -ProfileName $AWSProfile).Instances | Where-Object {$_.Tag.value -match $machineIn.DNSName.Split(".",2)[0]}
if ($instance.state.Name -eq 'stopped')
{
Write-Host $machineIn.DNSName.Split(".",2)[0] $instance.instanceid "(Starting Machine - AWS)"
if (!($LogOnly))
{
Try
{
Start-EC2Instance -ProfileName $AWSProfile -InstanceId $instance.instanceid | Out-Null
}
Catch
{
Write-Host $machineIn.DNSName.Split(".",2)[0] $instance.instanceid "(Unable to Start Instance - AWS)"
}
}
}
Else
{
Write-Host $machineIn.DNSName.Split(".",2)[0] "(Already Powered On - AWS)"
}
}
Catch
{
Write-Host $machineIn.DNSName.Split(".",2)[0] "(Unable to Get Instance - AWS)"
}
}
if ($Azure)
{
Write-host "Azure"
Try
{
$Instance = get-azureRMVM -Status | Where-Object {$_.Name -match $machineIn.DNSName.Split(".",2)[0]}
if ($instance.PowerState -eq 'VM deallocated')
{
Write-Host $machineIn.DNSName.Split(".",2)[0] $instance.name "(Starting Machine - Azure)"
if (!($LogOnly))
{
Try
{
Start-AzureRmVM -ResourceGroupName $Instance.ResourceGroupName -Name $Instance.Name | Out-Null
}
Catch
{
Write-Host $machineIn.DNSName.Split(".",2)[0] $instance.Name "(Unable to Start Instance - Azure)"
}
}
}
Else
{
Write-Host $machineIn.DNSName.Split(".",2)[0] "(Already Powered On - Azure)"
}
}
Catch
{
Write-Host $machineIn.DNSName.Split(".",2)[0] "(Unable to Get Instance - Azure)"
}
}
}
}
Else
{
Write-host "No machines to work with Inside Business hours, Check Tags"
}
}
Function Enter-ScheduleOut
{
Write-host "Outside Business Hours"
$machinesOut = $machines | Where-Object {$_.Tags -notcontains $IgnoreTag -and $_.Tags -notcontains $BaseTag}
if ($machinesOut)
{
Write-Host "Working on Machines:"
$machinesOut | Format-Table MachineName,DesktopGroupName,LoadIndex,RegistrationState,InMaintenanceMode,Tags
Foreach ($machineOut in $machinesOut)
{
#Maintenance Mode
Write-host $machineOut.DNSName.Split(".",2)[0] "(Setting Maintenance Mode - TRUE)"
if (!($LogOnly))
{
Try
{
$machineOut | Set-BrokerMachine -InMaintenanceMode $True | Out-Null
}
Catch
{
Write-host $machineOut.DNSName.Split(".",2)[0] "(Unable to put Machine in Maintenance Mode)"
}
}
#Power Off Machines
if ((Get-BrokerDesktop -AdminAddress $DeliveryController $machineOut.MachineName).InMaintenanceMode -eq 'True' -and !($machineOut.AssociatedUserNames))
{
if ($OnPrem)
{
Write-Host "OnPrem"
Write-host $machineOut.DNSName.Split(".",2)[0] "(Powering off Machine)"
if (!($LogOnly))
{
Try
{
New-BrokerHostingPowerAction -AdminAddress $DeliveryController -Action Shutdown -MachineName $machineOut.HostedMachineName | Out-Null
}
Catch
{
Write-host $machineOut.DNSName.Split(".",2)[0] "(Unable to Power Off Machine)"
}
}
}
if ($AWS)
{
Write-Host "AWS"
Try
{
$instance = (Get-EC2Instance -ProfileName $AWSProfile).Instances | Where-Object {$_.Tag.value -match $machineOut.DNSName.Split(".",2)[0]}
if ($instance.state.Name -eq 'running')
{
Write-host $machineOut.DNSName.Split(".",2)[0] $Instance.instanceid "(Powering off Machine - AWS)"
if (!($LogOnly))
{
Try
{
Stop-EC2Instance -ProfileName $AWSProfile -InstanceId $instance.instanceid | Out-Null
}
Catch
{
Write-Host $instance.instances.tag.value $Instance.instanceid "(Unable to Stop Instance - AWS)"
}
}
}
Else
{
Write-Host $machineOut.DNSName.Split(".",2)[0] "(Already Powered Down - AWS)"
}
}
Catch
{
Write-Host $machineOut.DNSName.Split(".",2)[0] "(Unable to Get Instance - AWS)"
}
}
if ($Azure)
{
Write-host "Azure"
Try
{
$Instance = get-azureRMVM -Status | Where-Object {$_.Name -match $machineOut.DNSName.Split(".",2)[0]}
if ($instance.PowerState -eq 'VM running')
{
Write-Host $machineOut.DNSName.Split(".",2)[0] $instance.name "(Powering off Machine - Azure)"
if (!($LogOnly))
{
Try
{
Stop-AzureRmVM -ResourceGroupName $Instance.ResourceGroupName -Name $Instance.Name -Force | Out-Null
}
Catch
{
Write-Host $machineOut.DNSName.Split(".",2)[0] $instance.Name "(Unable to Stop Instance - Azure)"
}
}
}
Else
{
Write-Host $machineOut.DNSName.Split(".",2)[0] "(Already Powered Down - Azure)"
}
}
Catch
{
Write-Host $machineOut.DNSName.Split(".",2)[0] "(Unable to Get Instance - Azure)"
}
}
}
else
{
Write-host $machineOut.DNSName.Split(".",2)[0] "(User/s logged on or Can't put Machine in MAINT mode, cannot Power Off Machine)"
}
}
}
Else
{
Write-host "No machines to work with Outside of Business hours, Check Tags"
}
}
Function Enter-LoadModeRemoveCapacity
{
Write-host "Enter LoadMode Remove"
$machinesLoad = $machines | Where-Object {$_.Tags -notcontains $IgnoreTag -and $_.Tags -notcontains $BaseTag}
if ($machinesLoad)
{
Write-Host "Working on Machines:"
$machinesLoad | Format-Table MachineName,DesktopGroupName,LoadIndex,RegistrationState,InMaintenanceMode,Tags
Foreach ($machineLoad in $machinesLoad)
{
}
}
}
Function Enter-LoadModeAddCapacity
{
Write-host "Enter LoadMode Add"
$machinesLoad = $machines | Where-Object {$_.Tags -notcontains $IgnoreTag -and $_.Tags -notcontains $BaseTag}
if ($machinesLoad)
{
Write-Host "Working on Machines:"
$machinesLoad | Format-Table MachineName,DesktopGroupName,LoadIndex,RegistrationState,InMaintenanceMode,Tags
Foreach ($machineLoad in $machinesLoad)
{
}
}
}
############ Email SMTP ###########
Function Email
{
$results = (Get-Content -Path $outputloc -raw)
$smtpserver = $SMTPserver
$msg = New-Object Net.Mail.MailMessage
$smtp = New-Object net.Mail.SmtpClient($smtpserver)
$msg.From = $FromAddress
$msg.To.Add($ToAddress)
$msg.Subject = "**Citrix IntelliScale Report - $DGName**"
$msg.body = "$results"
#$msg.Attachments.Add($att)
$smtp.Send($msg)
}
############ END Email SMTP ###########
###### Call out Functions ############
Get-IntelliMode
Write-host "-"
Write-host "******************************************"
###### End Call out Functions ############
####################### Closing ###########
$lastcomp = Get-date
$diff = ($lastcomp - $firstcomp)
Write-Host This Script took $diff.Minutes minutes and $diff.Seconds seconds to complete.
Write-Host "This Script Runs every hour from server: ($hostname)"
Write-host "******************************************"
Stop-Transcript
##############################################################
if ($Email) {Email}