In this post, I will walk you through the process of creating a custom report with PowerShell for current usage of an Azure resource. That way, you will be able to compare how many of your resources are in use and how much capacity is left.

Baki Onur Okutucu

Onur is a subject matter expert for Office 365, Azure, and PowerShell technologies. He is the founder of Clouderz Ltd, a cloud consultancy based in London. For eight years in a row, Microsoft has recognized him as a Most Valuable Professional. You can follow Onur on Twitter: @BakiOnur.

Defining Azure resources ^

Even though you've set your Azure resource limits to high values, it might still be critical to create consumption reports regularly. This is especially true for organizations that frequently create large numbers of resources in a dynamic way.

First, I'd like to list my current resources in a particular subscription:

This command shows us all resources in a subscription. However, this does not give us a complete report of all resource types and usage details.

Displaying all resources in a particular subscription

Displaying all resources in a particular subscription

In case we need to see how many virtual machines (VMs) are already in use, we can execute this command:

Total number of Azure Resource Manager (Azure RM) VMs

Total number of Azure Resource Manager (Azure RM) VMs

However, this command does not give us the maximum number of allowed VMs. You can view the limit for each resource available in Azure. But this is a huge list showing each resource with details for limits and quotas. The Azure PowerShell Module version 4.1.0 (or later) has the ability to list all resources with corresponding limit values.

Resource usage reports ^

Three cmdlets allow us to get a usage report for a particular Azure location. This point is important because each Azure location has its own separate limits, which may differ from one location to another. This is why we need to specify a value for the -Location parameter.

Current usage of Azure RM VMs in a specific Azure location

Current usage of Azure RM VMs in a specific Azure location



Current usage of Azure RM storage accounts, location independent

Current usage of Azure RM network resources in a specific Azure location

Current usage of Azure RM network resources in a specific Azure location

Combining reports ^

Now we should combine all three of the cmdlets above using custom formatting. We need to make sure the output for each cmdlet has the same column structure so we can have a single table showing all results in it. To achieve this, we'll get them aligned in a single view by giving columns identical names.

The column names are Name, CurrentValue, and Limit:

Now we are able to see all the results in a single table:

Combination of VM, storage, and network resource usage reports

With the help of the Out-Gridview cmdlet, we can display the output in an interactive table:

Grid view of the consumption report

By doing this, we've created a complete table listing all details we need in terms of resource limits and consumptions.

Reporting usage limits ^

Let's take this one step further by adding a usage check feature. For instance, you may want to get notified in case you start running out of VMs before you hit the limit. Even though Azure limits are not that restricted, we can make sure we have a sufficient amount of resources to use before it's too late. It makes sense to send a notification when 80 percent of a certain resource has been consumed. The script below checks the values and sends you a daily report with resource details using the Send-MailMessage cmdlet.

The code below will do the trick. Note: I've set the limit to 4% to be able to test it. You can change it to any value you like.

The script sends you a consumption report showing the resources that are hitting their limits.

An automatic Azure resource consumption report in your mailbox

An automatic Azure resource consumption report in your mailbox

Win the monthly 4sysops member prize for IT pros

Share
2+

Related Posts

3 Comments
  1. Hasan mohamed 1 month ago

    Hi,

    This script is fantastic, but I have problem in my scenario. I want to capture the Number of NSG Rules within the NSG. The command

    Get-AzureRmNetworkUsage -Location $location | select resourcetype,currentvalue,limit

    Result: I am NOT getting the actual currentvalue on the number of rules instead I get zero. But in portal I have 146 rules.

    Any help...

    Thanks,

    K. Hasan

    0

    • Author
      Baki Onur Okutucu 1 month ago

      Hi Hasan,

      Please use the following command to get the number of rules defined within a Network Security group

      ((Get-AzureRmNetworkSecurityGroup -Name "NSGName" -ResourceGroupName "ResourceGroupName").securityrules).count

      To be more specific, you can also narrow down the result specifying the direction (Inbound or Outbound)

      for Inbound Rules

      ((Get-AzureRmNetworkSecurityGroup -Name "NSGName" -ResourceGroupName "ResourceGroupName").securityrules | where{$_.direction -eq "Inbound"}).count

      for Outbound Rules

      ((Get-AzureRmNetworkSecurityGroup -Name "NSGName" -ResourceGroupName "ResourceGroupName").securityrules | where{$_.direction -eq "Outbound"}).count

      thanks

      Onur

      1+

      Users who have LIKED this comment:

      • avatar
  2. Hasan mohamed 4 weeks ago

    It works. Thanks

    0

Leave a reply

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

*

CONTACT US

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

Sending
© 4sysops 2006 - 2018

Log in with your credentials

or    

Forgot your details?

Create Account