Often times customer ask a unified and straightforward way to get the usage details of an Azure subscription programmatically. The Azure Powershel cmdlet Get-AzConsumptionUsageDetail can be used to find the usage detail with a number of parameters.
Select the correct subscription
Select-AzSubscription -SubscriptionId "c15e5bc8-977c-4dc4-9489-6a87265c1514"
Find usage between start and end date
You can run this command to find the usage details of a subscription between an specified date. For example to find the usage details during the last 30 days:
(Get-AzConsumptionUsageDetail -StartDate ([datetime]::Now.AddDays(-30)) -EndDate ([datetime]::Now.AddHours(0)))
Find usage by resource TAG + between start and end date
Sometimes you want to know the consumption across all resources under a subscription by tag name and value.
Unfortunately, the current implementation of this cmdlet has a bug that searching by tag is simply ignored. Here’s is the workaround implementation of the bug.
For example, you may want to find out the cost of all the resources which have tag name BillingId
set to 123456
. You can do so by first listing out all consumption usage details, then excludes the items which have no Tags defined, then look up for the tags of our interest, i.e. BillingId having value of 123456.
(Get-AzConsumptionUsageDetail -StartDate ([datetime]::Now.AddDays(-30)) -EndDate ([datetime]::Now.AddHours(0))) | Where-Object {$_.Tags -ne $null} | Where-Object {$_.Tags['BillingId'] -eq "123456"}
Future work: You can plan on applying on filters on any available properties.