-
Notifications
You must be signed in to change notification settings - Fork 1
/
Set-VMSSAutoScaling.ps1
105 lines (86 loc) · 3.25 KB
/
Set-VMSSAutoScaling.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
<#
Author: RubenFr
Creation Date: 1/10/2022
Comments:
Runs everyday in azure automation account.
This runbook activate autoscaling to all the VMSS that didn't configured it (Manual).
#>
Function Connect-Azure {
Connect-AzAccount `
-Identity `
-AccountId "USER MANAGED IDENTITY CLIENT ID"
}
Function Get-VMSSNotAutoScaled {
$autoScaleSettings = Get-AzResourceGroup |
% {
Get-AzAutoscaleSetting `
-ResourceGroupName $_.ResourceGroupName `
-WarningAction Ignore |
Select-Object Enabled, Name, TargetResourceUri
}
Write-Warning "Found $($autoScaleSettings.Count) AutoScale settings:"
Write-Warning $($autoScaleSettings | ConvertTo-Json)
$needAutoScale = Get-AzVmss |
Where-Object {
$_.Id -notin $autoScaleSettings.TargetResourceUri
}
return $needAutoScale
}
##############################################
############## Main ######################
##############################################
Connect-Azure
$subs = Get-AzSubscription | ? { $_.State -eq 'Enabled' } | Sort-Object Name
foreach ($sub in $subs) {
"`n$($sub.Name)"
Set-AzContext -SubscriptionId $sub.Id | Out-Null
$notScaledVmss = Get-VMSSNotAutoScaled
"Found $($notScaledVmss.count) VMSS to Autoscale"
foreach ($vmss in $notScaledVmss) {
$ruleScaleOut = New-AzAutoscaleRule `
-MetricName "Percentage CPU" `
-MetricResourceId $vmss.Id `
-TimeGrain 00:01:00 `
-MetricStatistic "Average" `
-TimeWindow 00:10:00 `
-Operator "GreaterThan" `
-Threshold 70 `
-ScaleActionDirection "Increase" `
-ScaleActionScaleType "ChangeCount" `
-ScaleActionValue 1 `
-ScaleActionCooldown 00:05:00
$ruleScaleIn = New-AzAutoscaleRule `
-MetricName "Percentage CPU" `
-MetricResourceId $vmss.Id `
-TimeGrain 00:01:00 `
-MetricStatistic "Average" `
-TimeWindow 00:10:00 `
-Operator "LessThan" `
-Threshold 20 `
-ScaleActionDirection "Decrease" `
-ScaleActionScaleType "ChangeCount" `
-ScaleActionValue 1 `
-ScaleActionCooldown 00:05:00
$scaleProfile = New-AzAutoscaleProfile `
-DefaultCapacity $([math]::Max(1, $vmss.Sku.Capacity)) `
-MinimumCapacity $([math]::Max(1, $vmss.Sku.Capacity)) ` # This keeps the current number of instances as the minimum
-MaximumCapacity $([math]::Max(10, $vmss.Sku.Capacity)) `
-Rule $ruleScaleOut, $ruleScaleIn `
-Name "$($vmss.Name)-autoscaleprofile"
} try {
Add-AzAutoscaleSetting `
-Location $vmss.Location `
-Name "$($vmss.Name)-autoscale" `
-ResourceGroup $vmss.ResourceGroupName `
-TargetResourceId $vmss.Id `
-AutoscaleProfile $scaleProfile `
-WarningAction Ignore `
-ErrorAction Stop |
Out-Null
"Autoscalled $($vmss.Name) (RG: $($vmss.ResourceGroupName))"
}
catch {
Write-Error "Error while adding autoscaling to $($vmss.Name) (RG: $($vmss.ResourceGroupName)):"
Write-Error $_
}
}