/
Get-AzureRMArmLimits.ps1
62 lines (50 loc) · 2.75 KB
/
Get-AzureRMArmLimits.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
<#
.SYNOPSIS
There are limits to the number of read/write operations that can be performed against the Azure Resource manager proviers in Azure.
When this limit is reached there will be an HTTP 429 error returned. The documentation below outlines the specific REST call but
does not provide a complete example
https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-request-limits
.DESCRIPTION
This script creates the proper bearer token to invoke the REST API on the number of remaining Read Operations allowed against a specific
subscription. The function Get-AzureCachedAccessToken provides the logic to pull the access token required to pass into the REST API
Version modified to accomodate Azure Stack Hub environments
#>
function Get-AzureRmCachedAccessToken()
{
$ErrorActionPreference = 'Stop'
if(-not (Get-Module AzureRm.Profile)) {
Import-Module AzureRm.Profile
}
$azureRmProfileModuleVersion = (Get-Module AzureRm.Profile).Version
# refactoring performed in AzureRm.Profile v3.0 or later
if($azureRmProfileModuleVersion.Major -ge 3) {
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
if(-not $azureRmProfile.Accounts.Count) {
Write-Error "Ensure you have logged in before calling this function."
}
} else {
# AzureRm.Profile < v3.0
$azureRmProfile = [Microsoft.WindowsAzure.Commands.Common.AzureRmProfileProvider]::Instance.Profile
if(-not $azureRmProfile.Context.Account.Count) {
Write-Error "Ensure you have logged in before calling this function."
}
}
$currentAzureContext = Get-AzureRmContext
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient($azureRmProfile)
Write-Debug ("Getting access token for tenant" + $currentAzureContext.Subscription.TenantId)
$token = $profileClient.AcquireAccessToken($currentAzureContext.Subscription.TenantId)
$token.AccessToken
}
Write-Host "Log in to your Azure subscription..." -ForegroundColor Green
#Login-AzureRmAccount
#Get-AzureRmSubscription -SubscriptionName $SubscriptionName | Select-AzureRmSubscription
$token = Get-AzureRmCachedAccessToken
$currentAzureContext = Get-AzureRmContext
Write-Host ("Getting access ARM Throttle Limits for Subscription: " + $currentAzureContext.Subscription)
$requestHeader = @{
"Authorization" = "Bearer " + $token
"Content-Type" = "application/json"
}
$Uri = ('{0}/subscriptions/{1}/resourcegroups?api-version=2016-09-01' -f $currentAzureContext.Environment.ResourceManagerUrl, $currentAzureContext.Subscription.Id)
$r = Invoke-WebRequest -Uri $Uri -Method GET -Headers $requestHeader
write-host("Remaining Read Operations: " + $r.Headers["x-ms-ratelimit-remaining-subscription-reads"])