Skip to content

Instantly share code, notes, and snippets.

@shawnweisfeld
Created July 14, 2020 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawnweisfeld/62a66825ef1f87334f5b494893837834 to your computer and use it in GitHub Desktop.
Save shawnweisfeld/62a66825ef1f87334f5b494893837834 to your computer and use it in GitHub Desktop.
# Get metrics for all storage accounts in an azure sub
## Fill in the shared variables
$sub = "" # Azure Subscription GUID
## The script
$metrics = @()
az login
# list all my resource groups in this sub
$rgs = az group list --subscription $sub | ConvertFrom-Json
foreach ($rg in $rgs)
{
# list all storage accounts in this resource group
$accounts = az storage account list --subscription $sub --resource-group $rg.name | ConvertFrom-Json
foreach ($acct in $accounts)
{
$resource = "/subscriptions/" + $sub + "/resourceGroups/" + $rg.name + "/providers/Microsoft.Storage/storageAccounts/" + $acct.name
Write-Host "Getting Metrics for $resource"
# export the metrics to a json object
$metric = az monitor metrics list `
--resource $resource `
--end-time ([System.DateTime]::UtcNow).ToString("O") `
--offset 1d `
--interval 1h --metric UsedCapacity Transactions Ingress Egress | ConvertFrom-Json
# convert the json object to a reportable object
foreach ($mv in $metric.value)
{
foreach ($ts in $mv.timeseries)
{
foreach ($tsd in $ts.data)
{
$metrics += [pscustomobject]@{
Subscription=$sub
ResourceGroup=$rg.name
StorageAccount=$acct.name
Metric=$mv.name.value
Unit=$mv.unit
Value=$tsd.average
TimeStamp=$tsd.timeStamp
}
}
}
}
}
}
Write-Host "Saving Results File"
$metrics | Format-Table > "results.txt"
$metrics | Export-csv -Path ".\results.csv"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment