Luckily, we can do a few things to keep tabs on how much our Azure bill is going to be. One way is with PowerShell. Building a usage reporting tool in PowerShell lets you add this to a larger automation script or create your own scheduled monitors.

Microsoft Azure is great until it hits you with a huge bill you weren't expecting! Like it or not, cloud costs are not static and change from month to month. Azure is an awesome way to bring up resources quickly, but just don't forget about them! You may end up incurring some unexpected bills.

Creating an Azure usage report tool in PowerShell obviously requires you to have an Azure account and at least one virtual machine. You should also have the AzureRM PowerShell module available for download; install it by running Install-Module AzureRm. Once you've installed the PowerShell module, log in with Connect-AzureRmAccount, and you're good to go.

The main cmdlet we'll be using to pull information about usage is Get-UsageAggregates. This command is in the AzureRm module. Calling it returns a ton of information about all of your Azure resources. It has four main parameters we will be using:

  • ReportedStartTime indicates the earliest time to pull usage information from
  • ReportedEndTime indicates the latest time to pull usage information from
  • AggregationGranularityindicates how to group the usage information, such as Daily or Monthly
  • ShowDetails tells the command to return more granular, instance-level details

Below, I'm pulling out two random dates I know have usage data associated with them to see what I can find.

When you look at the $usageData variable, you'll immediate get a ton of usage information back.

Azure usage

Azure usage

The default output isn't too useful, but if you drill down into the UsageAggregations property, you'll begin to see a little more structure to the output. We're getting closer, but if I find out the number of objects returned, I can see it is a flat 1,000. It's not very coincidental it returns exactly 1,000 objects. I have a feeling there's a limit.

It turns out that Get-UsageAggregates only returns 1,000 objects by default. It uses a continuation token that each call returns. You then need to pass this to a subsequent call to get the next set of data. You can see this token by looking at the ContinuationToken property returned.

To get the rest of the information, we need to use this continuation token and pass it to Get-UsageAggregates again using the ContinuationToken parameter like below.

This goes on until I finally get all the usage data that exists between those two dates—not too much fun.

To avoid having to do a lot of manual copying and pasting, we can create our own Azure usage function that automatically detects if this continuation token came from the output indicating there's more data available. It then calls the Get-UsageAggregates function again with the continuation token. Below is an example of such a function.

Creating a custom function not only lets us account for that continuation token but also lets us create our own parameters, validation, and all the other controls that building our own functions gives us!

If you bring this function into your PowerShell session, you can then execute it by providing the same start/end times and intervals. However, notice the parameters to execute this command are a little different this time. They are FromTime, ToTime, and Interval. These are a little more descriptive than ReportedStartTime, ReportedEndTime, and AggregrationGranularity.

The command will run continuously until it finds all the usage information. It will return the structured output we need by only returning the value of the UsageAggregations property as described earlier.

You can see below that you receive some verbose output when running this command. But we didn't get any kind of usage information since the output is going to the $usage variable.

Looking at the $usage variable now shows the generated report returns tons of information about our usage.

Inspecting Azure usage

Inspecting Azure usage

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

0
Share
0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account