Search code examples
azurepowershellazure-storageazure-powershellazure-storage-account

Get Storage Account size using Powershell using AZMetrics often returns 0


I'm using the AZ metrics command found in the answer posted below which usually works but every so often the result returns 0, even though I know the account is consuming storage. Also, if I run the same command against the same resource a little while later it returns the real result. Is anyone else using this method and finding similar results? Any suggestions to get around this?

Also, is it possible to use timegrain/timespan against a Storage Account?

List used space for blobs, files, tables, queues using PowerShell?

For reference, the command I'm using is: (Get-AzMetric -ResourceId "{resource_id}" -MetricName "UsedCapacity").Data


Solution

  • I tried to reproduce the same in my environment and faced the same issue.

    enter image description here

    When I ran the command again after some time, got null value like below:

    enter image description here

    After a while, when I ran the above command again, it gave the response successfully.

    This issue usually happens because capacity metrics values will be refreshed daily (up to 24 Hours).

    • You can turn on the debugging to know what exactly is going wrong.
    • To turn on, use $DebugPreference = "Continue".
    • You can use Get-AzMetricDefinition to know the time intervals that are supported.

    As a workaround, you can make use of timegrain/timespan against the storage account like below:

    (Get-AzMetric -ResourceId "your_resource_id" -MetricName "UsedCapacity" -AggregationType Average -StartTime "02:00:00" -EndTime "04:00:00").Data
    

    If still the issue persists, raise a bug/Azure support ticket.

    References:

    0-Value Result Get-AzMetric (UsedCapacity) · Issue #15863 · Azure/azure-powershell · GitHub

    Get-AzMetric Returns Blank Results - githubmemory