Last active
April 23, 2021 20:51
-
-
Save k-rush/6ddb6c05168c2e23709fb18e5d585804 to your computer and use it in GitHub Desktop.
List all storage accounts for a resource group, get the maximum ingress and egress per minute for the past 30 days
This file contains 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]$SubscriptionId, | |
[String]$ResourceGroupName | |
) | |
$nowTimestamp = Get-Date -Format o | |
$30DaysAgoTimestamp = (Get-Date).AddDays(-30) | Get-Date -Format o | |
function Get-MaxMetric | |
{ | |
param ( | |
$ResourceId, | |
$MetricType | |
) | |
Write-Host "Getting max ${MetricType} for ${ResourceId}" | |
$request = "${ResourceId}/providers/microsoft.insights/metrics?interval=PT1M×pan=${30DaysAgoTimestamp}/${nowTimestamp}&metricnames=${MetricType}&api-version=2019-07-01&aggregation=Total" | |
$response = armclient GET $request | ConvertFrom-Json | |
$max = $response.value[0].timeseries[0].data | Measure-Object -Property total -Maximum | |
return $max.Maximum | |
} | |
armclient login | |
$storageAccounts = armclient GET "subscriptions/${SubscriptionId}/resourceGroups/${ResourceGroupId}/providers/Microsoft.Storage/storageAccounts?api-version=2021-01-01" | ConvertFrom-Json | |
$storageAccountIds = $storageAccounts.value.id | |
$storageMaxIngressAndEgress = @() | |
$storageAccountIds | ForEach-Object -Process { | |
$maxIngress = Get-MaxMetric -ResourceId $_ -MetricType "Ingress" | |
$maxEgress = Get-MaxMetric -ResourceId $_ -MetricType "Egress" | |
$storageMaxIngressAndEgress += [PSCustomObject]@{ | |
resourceId=$_ | |
maxIngressInGb=$maxIngress * 0.000000001 | |
maxEgressInGb=$maxEgress * 0.000000001 | |
} | |
} | |
$csvPath = ".\storageAccountUsage.csv" | |
$storageMaxIngressAndEgress | Export-Csv -Path $csvPath | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment