-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.ps1
63 lines (58 loc) · 2.2 KB
/
script.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
param(
[Parameter(mandatory=$true)]
[string]$scalingPlan,
[Parameter(mandatory=$true)]
[string]$scalingPlanRG,
[Parameter(mandatory=$true)]
[string]$hostPoolRG,
[Parameter()]
[String]$subscription = "your-subscription-name-here",
[Parameter(mandatory=$true)]
[String[]]$hostPools,
[Parameter(mandatory=$true)]
[bool]$scalingPlanEnabled,
[Parameter(mandatory=$true)]
[bool]$startVMs,
[Parameter(mandatory=$true)]
[string]$patchWindow,
[Parameter(mandatory=$true)]
[bool]$patchingEnabled
)
<#
Before patching set $scalingPlanEnabled to $false and $startVMs to $true to let VMs running before patch window
After patching is over set $scalingPlanEnabled back to $true and set $startVMs to $false
#>
if ($patchingEnabled) {
# Login to Azure
Connect-AzAccount -Identity
# Select subscription scope
Select-AzSubscription $subscription
# Exist if no hostPools provided
if ($hostPools.Count -eq 0) {
throw "No hostPool(s) provided."
}
# Get HostPoolReferences for the selected scalingplan
$hpRef = Get-AzWvdScalingPlan -Name $scalingPlan -ResourceGroupName $scalingPlanRG | select *
# Iterate through all hostpoolreferences
foreach ($ref in $hpRef.HostPoolReference) {
# If hostpool is in provided hostPool array, change scaling plan enabled to the selected setting
if ($ref.HostPoolArmPath.split("/")[-1] -in $hostPools) {
$ref.ScalingPlanEnabled = $scalingPlanEnabled
}
}
# Update scaling plan settings. This will enable or disable autoscale for the selected hostpools using the provided scale plan.
Update-AzWvdScalingPlan -Name $scalingPlan -ResourceGroupName $scalingPlanRG -HostPoolReference $hpRef.HostPoolReference
# Iterate through each hostpool and start all hosts.
# Additionally enable drainmode using the same variable used for enabling/disabling the scalingPlan.
foreach ($hp in $hostPools) {
Get-AzWvdSessionHost -HostPoolName $hp -ResourceGroupName $hostPoolRG | select * | % {
if ($startVMs) {
Start-AzVM -Id $_.ResourceId
}
$vm = Get-AZVM -ResourceId $_.ResourceId
if ($vm.Tags['vm-update'] -eq $patchWindow) {
Update-AzWvdSessionHost -ResourceGroupName $hostPoolRG -HostPoolName $hp -Name $_.Name.split("/")[-1] -AllowNewSession:$scalingPlanEnabled
}
}
}
}