-
Notifications
You must be signed in to change notification settings - Fork 1
/
Runbook.ps1
111 lines (81 loc) · 4.05 KB
/
Runbook.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
<#
Modules: Az.Accounts , Az.Compute, Az.ResourceGraph
#>
############################################################### VARIABLES #############################################################################
<# % to Trigger the increase #>
$Trigger = 20
<# % to increase #>
$Increase = 20
<# Subscriptions to run #>
$TargetSubscription = '3d5f753d-ef56-4b30-97c3-fb1860e8f44c','c44908f1-44a3-47f7-aae3-fbd1c0457f9c'
############################################################### READING #############################################################################
Clear-AzContext -Force
Connect-AzAccount -Identity
$Subscriptions = Get-AzContext -ListAvailable | Where-Object {$_.Subscription.State -ne 'Disabled' -and $_.Subscription.SubscriptionId -in $TargetSubscription}
Write-Output ('Running in: '+$Subscriptions.count+' Subscriptions.')
$Subscriptions = $Subscriptions.Subscription
$Location = Search-AzGraph -Query "resources | where type == 'microsoft.compute/virtualmachines' or type == 'microsoft.compute/virtualmachinescalesets' | summarize by location"
$Quotas = @()
foreach($sub in $Subscriptions)
{
Set-azContext -Subscription $sub | Out-Null
foreach($Loc in $Location)
{
$Quota = Get-AzVMUsage -Location $Loc.location
$Quota = $Quota | Where-Object {$_.CurrentValue -ge 1}
$Q = @{
'Location' = $Loc;
'Subscription' = $Sub;
'Data' = $Quota
}
$Quotas += $Q
}
}
################################################################ PROCESSING #######################################################################################
$Token = Get-AzAccessToken
$Token = $Token.Token
$headers = @{
Authorization="Bearer $Token"
}
foreach($Quota in $Quotas)
{
foreach($Data in $Quota.Data)
{
if($Data.Name.LocalizedValue -like '*Family vCPUs')
{
$PvCPU = ($Data.currentValue / $Data.limit)*100
Write-Output ('')
Write-Output ('Validating Quota for the Subscription: '+$Quota.Subscription+' in the location: '+[string]$Quota.Location.location)
if($PvCPU -ge $Trigger)
{
Write-Output ('---------------------------------------')
$Name = $Data.Name.value
Write-Output ('Requesting Increase of Quota for Family: '+$Name +'. For Location: '+[string]$Quota.Location.location)
$NewLimit = $Data.limit + (($Data.limit/100)*$Increase)
Write-Output ('Old Quota: '+$Data.limit. '| New Quota: '+$NewLimit)
$Body = @"
{
"properties": {
"limit": $NewLimit,
"unit": "Count",
"name": {
"value": "$Name"
}
}
}
"@
$Sub = [string]$Quota.Subscription.id
$Locate = [string]$Quota.Location.location
$FamilyName = ([string]$Data.Name.value+'?api-version=2020-10-25')
$Uri = "https://management.azure.com/subscriptions/$Sub/providers/Microsoft.Capacity/resourceProviders/Microsoft.Compute/locations/$Locate/serviceLimits/$FamilyName"
<# Comment the following line if you want to just validate the script and not request the Quota increse #>
$Request = Invoke-WebRequest -Uri $Uri -Headers $headers -Body $Body -Method Put -UseBasicParsing
Write-Output ('Request Status: '+$Request.StatusCode)
}
else
{
Write-Output ('No Quota increase was needed for Subscription: '+$Quota.Subscription+' in the location: '+[string]$Quota.Location.location)
}
}
}
}