Azure Adventures #3
We are now on the fourth Azure Adventure post and we covered a lot of basic tenant and user profile information. In this article we will explore Billing and usage as well as anything to do with consumption of services in Azure. One item to be aware of is that traditional Azure Subscriptions and MSDN Azure subscriptions have no problems displaying information queried by these cmdlets. However, at least one particular subscription type will not display and this is a sponsored Azure subscription provided by Microsoft. If you have one of these subscriptions, then details for this section of cmdlets will not be displayed. At this point in time, Microsoft is not providing this information for these sponsored subscriptions. There is a public announcement about this changing, but no real details about the how, why, etc at the moment.
Azure Billing Module
This module within Azure PowerShell is not a huge module per say, but can be very informative depending on what’s enabled in an Azure tenant. Let’s just dive into the PowerShell:
PowerShell Cmdlets
[sourcecode language="powershell"] Get-Command |Where {$_.Source -like 'az.billing'}
Which reveals these cmdlets:
Get-AzBillingInvoice Get-AzBillingPeriod Get-AzConsumptionBudget Get-AzConsumptionMarketplace Get-AzConsumptionPriceSheet Get-AzConsumptionReservationDetail Get-AzConsumptionReservationSummary Get-AzConsumptionUsageDetail Get-AzEnrollmentAccount Get-UsageAggregates New-AzConsumptionBudget Remove-AzConsumptionBudget Set-AzConsumptionBudget
These cmdlets do tie into some of the concepts and cmdlets that we explored last week. In particular we may need to work with the *-AzSubscription cmdlets in order to get an accurate representation from a tenant, if there are multiple Azure subscriptions. If there is only one subscription, then this is of course a moot point. For quick review, we can reveal our subscriptions with Get-AzSubscription which will provide a table of the Subscription Name and ID. In order to change Azure subscriptions, use Select-AzSubscription -Subscription
Exploring the Billing Module
Once we are connected to Azure and the correct subscription we can now work with these cmdlet. First up is the Get-AZBillingInvoice cmdlet. The cmdlet’s purpose is pretty self explanatory in that it retrieves billing Invoices for this subscription:
Get-AzBillingInvoice
Well, it’s not really that revealing per say so what can we really do with this cmdlet? First we check out the parameters available to us in help.
Get-Help Get-AzBillingInvoice -Parameters
From this we see that there are parameters that we can use to tweak what is return, but more importantly, provide us a way to get to our invoices.
GenerateDownloadUrl: Provides a URL for the Invoice to download and review
Latest: Shows only the last invoice
MaxCount: Return a maximum number of invoices
Name: Name of the Invoice to show
First, let’s just check out our latest invoice:
Get-AzBillingInvoice -Latest
This provides details on the latest Azure subscription invoice, including a download link:
When we click on the download link, we are provided with a detailedbill for our subscription. Below is a sample bill [3 pages] from an inactive test tenant for representation purposes:
Now in my case this is part of an MSDN benefit, so the billing payment is ‘free’ so YMMV. In my testing, I was only able reveal the last 7 invoices as well. you can specify a greater number in MaxCount, but no more invoices will be revealed. Again, you may experience different results as the cmdlet Examples show a ‘MaxCount 10’.
Get-AzBillingPeriod reveals information on billing periods for a tenant as well as the relevant invoice, but that’s all:
Our next cmdlet, Get-AzConsumptionBudget, will not reveal much in a default or greenfield tenant as there will be no Consumption Budgets to display. We would first need to create an AZ Consumption Budget using the New-AzConsumptionBudget cmdlet in order to be able to list any budgets. (Seems logical!)
However, this cmdlet seems a bit broken at the moment. Using the GUI may be better for your experience. I will update this section once I can actually add a budget this way. See this article for references:
https://github.com/Azure/azure-powershell/issues/9819
Get-AzConsumptionMarketplace – this would seem to imply that any Azure Marketplace items will show when this cmdlet s run (note there are no other cmdlets for the Azure Marketplace). In a new tenant or Greenfield tenant, no items will be displayed.
Get-AzConsumptionPriceSheet – also not working in my tenant:
These cmdlets reveal any Reservations that have been purchased for an Azure Subscription:
Get-AzConsumptionReservationDetail Get-AzConsumptionReservationSummary
Find more out HERE
Get-AzConsumptionUsageDetail
Quick run of the cmdlet reveals this:
Now, we additionally have the following interesting Parameters and Switches:
BillingPeriodName <System.String> EndDate <System.Nullable`1[System.DateTime]> Expand <System.String> IncludeAdditionalProperties <System.Management.Automation.SwitchParameter> IncludeMeterDetails <System.Management.Automation.SwitchParameter> InstanceId <System.String> InstanceName <System.String> MaxCount <System.Nullable`1[System.Int32]> ResourceGroup <System.String> StartDate <System.Nullable`1[System.DateTime]> Tag <System.String> Top <System.Nullable`1[System.Int32]>
What does the -IncludeMeterDetails switch do? Let’s try it out two sample one-liners
# (left side) Get-AzConsumptionUsageDetail # (right side) Get-AzConsumptionUsageDetail -IncludeMeterDetails
Notice the value of ‘Microsoft.Azure.Management.Consumption.Models.MeterDetails’.
Get-AzConsumptionUsageDetail -IncludeMeterDetails $Details = @() Foreach ($Line in $AllConsumption){ $Detail = $Line.MeterDetails $Details += $Detail } $Details | Ft
We have options to retrieve the top consumers with the ‘-Top’ Parameter and specifying an integer like 5, 10 or 20 (as examples. We can also explore a particular time period with ‘-EndDate’ and ‘-StartDate’. We also have an option to look at a particular billing period, Tag or ResourceGroup from which to pull consumption information.
Resource usage on a per Resource Group basis:
$AZRGs = (Get-AzResourceGroup).ResourceGroupName Foreach ($AZRG in $AZRGs){ Write-Host "Consumption Usage Detail - $AZRG" -ForegroundColor Yellow Get-AzConsumptionUsageDetail -ResourceGroup $AZRG }
Results:
Time Period
We can also list the usage during a specified time frame. This requires the usage of ‘EndDate’ and ‘StartDate’ values. If we want to get usage for the month of January so we construct a query like this:
Get-AzConsumptionUsageDetail -StartDate '1/1/2020' -EndDate '1/31/2020'
What is strange, at least strange to me, is that the output from this time period is not in any sort of date/time order:
UsageStart
———-
1/9/2020 0:00
1/30/2020 0:00
1/29/2020 0:00
1/28/2020 0:00
1/3/2020 0:00
1/2/2020 0:00
1/1/2020 0:00
1/8/2020 0:00
Lastly, we have the Get-UsageAggregates cmdlet. Microsoft has this for a description of the cmdlet:
“The Get-UsageAggregates cmdlet gets aggregated Azure subscription usage data by the following properties:
– Start and end times of when the usage was reported.
– Aggregation precision, either daily or hourly.
– Instance level detail for multiple instances of the same resource.“
If we run the cmdlet, we get some other details about our Azure assumption:
CONCLUSION
While this is a smaller module in the Azure PowerShell space, it does provide some good insight into our Azure tenant. We can use these cmdlets to see what is going on consumption as well as produce some interesting reports. Look for updates to this article later in the year once I resolve and get some more results from the Marketplace, Reservation and Budget cmdlets. In my next article, I will cover more on virtual machines.