Skip to content

Commit 1a85482

Browse files
Create CloudShell.ps1
1 parent 2d5ec88 commit 1a85482

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

CloudShell.ps1

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Use Goal 1 to scan Quotas and Goal 2 to request increase
2+
$Goal = 1
3+
4+
$Sub = 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
5+
$Location = 'brazilsouth'
6+
$VMFamily = 'standardBSFamily'
7+
$Quota = 200
8+
9+
if($Goal -eq 1)
10+
{
11+
write-host "Scanning used Quotas"
12+
$Quota = Get-AzVMUsage -Location $Location
13+
$Quota = $Quota | Where-Object {$_.CurrentValue -ge 1 -and $_.Name.LocalizedValue -like '*vCPUs'}
14+
$Quota | Format-Table -property @{label='Name';e={$_.Name.LocalizedValue}}, @{label='Family Name';e={$_.Name.Value}}, CurrentValue, Limit
15+
}
16+
17+
if($Goal -eq 2)
18+
{
19+
write-host ("Starting to request Quota Increase for: " + $Sub)
20+
write-host ("Requesting Quota for VM Family: " + $VMFamily)
21+
write-host ("Requesting Quota for Location: " + $Location)
22+
write-host ("Requesting Quota increase to: " + $Quota)
23+
write-host "Authenticating..."
24+
############################################################### AUTHENTICATE #############################################################################
25+
26+
Clear-AzContext -Force
27+
Connect-AzAccount -UseDeviceAuthentication
28+
Set-azContext -Subscription $Sub
29+
30+
################################################################ PROCESSING #######################################################################################
31+
32+
write-host "Creating headers..."
33+
$Token = Get-AzAccessToken
34+
$Token = $Token.Token
35+
$headers = @{
36+
Authorization="Bearer $Token"
37+
}
38+
39+
write-host "Creating body..."
40+
$Body = @"
41+
{
42+
"properties": {
43+
"limit": {
44+
"limitObjectType": "LimitValue",
45+
"value": $Quota
46+
},
47+
"name": {
48+
"value": "$VMFamily"
49+
}
50+
}
51+
}
52+
"@
53+
54+
write-host "Recording Family Name..."
55+
$FamilyName = ([string]$VMFamily+'?api-version=2021-03-15-preview')
56+
57+
#$Uri = "https://management.azure.com/subscriptions/$Sub/providers/Microsoft.Capacity/resourceProviders/Microsoft.Compute/locations/$Location/providers/Microsoft.Quota/quotas/$FamilyName"
58+
59+
write-host "Creating Resource ID..."
60+
61+
$RUI = "subscriptions/$Sub/providers/Microsoft.Compute/locations/$Location"
62+
63+
write-host "Recording URI..."
64+
65+
$Uri = "https://management.azure.com/$RUI/providers/Microsoft.Quota/quotas/$FamilyName"
66+
67+
write-host "Invoking REST API..."
68+
69+
$Response = Invoke-WebRequest -Uri $Uri -Headers $headers -Body $Body -Method Put -UseBasicParsing
70+
71+
$Response = $Response.Content | convertfrom-Json
72+
73+
write-host ("Quota Increase Request Created, Request ID: "+$Response.name)
74+
75+
$ResponseName = $Response.name
76+
77+
write-host "Creating Quota Increase Status Request..."
78+
79+
$Request = ('https://management.azure.com/'+ $RUI + '/providers/Microsoft.Quota/quotaRequests/'+ $ResponseName +'?api-version=2021-03-15-preview')
80+
81+
write-host "Invoking REST API to get Request Status..."
82+
83+
$Status = Invoke-WebRequest -Uri $Request -Headers $headers -Method Get -UseBasicParsing
84+
85+
$Status = $Status.content | convertfrom-json
86+
87+
write-host ("Response: "+ $Status.properties.message)
88+
89+
while($Status.properties.message -like "*Request processing*")
90+
{
91+
write-host "Waiting Request to be completed..."
92+
start-sleep 30
93+
$Status = Invoke-WebRequest -Uri $Request -Headers $headers -Method Get -UseBasicParsing
94+
$Status = $Status.content | convertfrom-json
95+
}
96+
97+
write-host ("Request Status: "+ $Status.properties.message)
98+
if($Status.error.code)
99+
{
100+
write-host ("Error Message: "+$Status.error.code)
101+
}
102+
103+
write-host "End."
104+
}

0 commit comments

Comments
 (0)