Skip to content

Instantly share code, notes, and snippets.

@k-rush
Last active April 23, 2021 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k-rush/6ddb6c05168c2e23709fb18e5d585804 to your computer and use it in GitHub Desktop.
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
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&timespan=${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