Created
July 23, 2019 17:46
-
-
Save vnbaaij/fde7488d09d086ee895ff473b46b6e1a to your computer and use it in GitHub Desktop.
PowerShell script to collect usage limits and quota information from Azure subscription
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[string]$workspaceId, | |
[string]$sharedKey, | |
[string[]]$locations | |
) | |
$connectionName = "AzureRunAsConnection" | |
try | |
{ | |
# Get the connection "AzureRunAsConnection " | |
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName | |
#"Logging in to Azure..." | |
$connectionResult = Connect-AzAccount -Tenant $servicePrincipalConnection.TenantID ` | |
-ApplicationId $servicePrincipalConnection.ApplicationID ` | |
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint ` | |
-ServicePrincipal | |
#"Logged in." | |
} | |
catch { | |
if (!$servicePrincipalConnection) | |
{ | |
$ErrorMessage = "Connection $connectionName not found." | |
throw $ErrorMessage | |
} else{ | |
Write-Error -Message $_.Exception | |
throw $_.Exception | |
} | |
} | |
$LogType = "AzureQuota" | |
# Choose between all locations or only a fixed set | |
# $locations = (Get-AzLocation | Select-Object Location).Location | |
# $locations = ["southcentralus","northeurope","westeurope"] | |
$sub = (Get-AzContext).Subscription.Name | |
$json = '' | |
# Get VM quotas | |
foreach ($location in $locations) | |
{ | |
$vmQuotas = Get-AzVMUsage -Location $location | |
foreach($vmQuota in $vmQuotas) | |
{ | |
$usage = 0 | |
if ($vmQuota.Limit -gt 0) { $usage = $vmQuota.CurrentValue / $vmQuota.Limit } | |
$json += @" | |
{ "SubscriptionName":"$sub","Name":"$($vmQuota.Name.LocalizedValue)", "Category":"Compute", "Location":"$location", "CurrentValue":$($vmQuota.CurrentValue), "Limit":$($vmQuota.Limit),"Usage":$usage }, | |
"@ | |
} | |
} | |
# Get Network Quota | |
foreach ($location in $locations) | |
{ | |
$networkQuotas = Get-AzNetworkUsage -location $location | |
foreach ($networkQuota in $networkQuotas) | |
{ | |
$usage = 0 | |
if ($networkQuota.limit -gt 0) { $usage = $networkQuota.currentValue / $networkQuota.limit } | |
$json += @" | |
{ "SubscriptionName":"$sub","Name":"$($networkQuota.name.localizedValue)", "Category":"Network", "Location":"$location", "CurrentValue":$($networkQuota.currentValue), "Limit":$($networkQuota.limit),"Usage":$usage }, | |
"@ | |
} | |
} | |
foreach ($location in $locations) | |
{ | |
# Get Storage Quota | |
$storageQuotas = Get-AzStorageUsage -location $location | |
foreach ($storageQuota in $storageQuotas) | |
{ | |
$usage = 0 | |
if ($storageQuota.Limit -gt 0) { $usage = $storageQuota.CurrentValue / $storageQuota.Limit } | |
$json += @" | |
{ "SubscriptionName":"$sub","Name":"$($storageQuota.LocalizedName)", "Location":"$location", "Category":"Storage", "CurrentValue":$($storageQuota.CurrentValue), "Limit":$($storageQuota.Limit),"Usage":$usage }, | |
"@ | |
} | |
} | |
# Wrap in an array | |
$json = $json.TrimEnd(",") | |
$json = "[$json]" | |
# Create the function to create the authorization signature | |
Function Build-Signature ($workspaceId, $sharedKey, $date, $contentLength, $method, $contentType, $resource) | |
{ | |
$xHeaders = "x-ms-date:" + $date | |
$stringToHash = $method + "`n" + $contentLength + "`n" + $contentType + "`n" + $xHeaders + "`n" + $resource | |
$bytesToHash = [Text.Encoding]::UTF8.GetBytes($stringToHash) | |
$keyBytes = [Convert]::FromBase64String($sharedKey) | |
$sha256 = New-Object System.Security.Cryptography.HMACSHA256 | |
$sha256.Key = $keyBytes | |
$calculatedHash = $sha256.ComputeHash($bytesToHash) | |
$encodedHash = [Convert]::ToBase64String($calculatedHash) | |
$authorization = 'SharedKey {0}:{1}' -f $workspaceId,$encodedHash | |
return $authorization | |
} | |
# Create the function to create and post the request | |
Function Post-LogAnalyticsData($workspaceId, $sharedKey, $body, $logType) | |
{ | |
$method = "POST" | |
$contentType = "application/json" | |
$resource = "/api/logs" | |
$rfc1123date = [DateTime]::UtcNow.ToString("r") | |
$contentLength = $body.Length | |
$signature = Build-Signature ` | |
-workspaceId $workspaceId ` | |
-sharedKey $sharedKey ` | |
-date $rfc1123date ` | |
-contentLength $contentLength ` | |
-method $method ` | |
-contentType $contentType ` | |
-resource $resource | |
$uri = "https://" + $workspaceId + ".ods.opinsights.azure.com" + $resource + "?api-version=2016-04-01" | |
$headers = @{ | |
"Authorization" = $signature; | |
"Log-Type" = $logType; | |
"x-ms-date" = $rfc1123date; | |
} | |
$response = Invoke-WebRequest -Uri $uri -Method $method -ContentType $contentType -Headers $headers -Body $body -UseBasicParsing | |
return $response.StatusCode | |
} | |
# Submit the data to the API endpoint | |
Post-LogAnalyticsData -workspaceId $workspaceId -sharedKey $sharedKey -body ([System.Text.Encoding]::UTF8.GetBytes($json)) -logType $logType | |
# uncomment next line to see JSON in output | |
#$json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment