Azure provides a Usage API that allows you to retrieve detailed usage information for your Azure resources. This information can be valuable for monitoring and optimizing your resource consumption, tracking costs, and understanding the usage patterns of your Azure services.
To access the Azure usage report using PowerShell, you can utilize the Azure PowerShell module (Az). This module provides cmdlets that allow you to interact with various Azure services, including retrieving usage details.
How to Download Usage Report With Azure Account Center
The Azure Account Center offers a variety of user data that you can download. You may check the history of your billing cycles, obtain invoices, and get an overview of your Azure membership by visiting the Azure Account Center, a website.
- The date range cannot be altered in any way. Because the dates are set to correspond with your payment cycle, you cannot restrict usage to a certain timeframe.
- There is a ton of additional information in use reports that you might not need.
- Perhaps you require usage statistics over the previous 60 days.
To cover all of your needed dates, you would need to download usage records for at least three billing cycles. - You would then need to remove the extra data once the reports had been downloaded. The next step would be to devise a strategy for compiling such data.
- You can create a reusable script, function, or module in PowerShell to pull Azure resource consumption reports. These can then be executed manually using a few lines of code or automatically as a scheduled task.
First Get Azure Resource Usage Data Using PowerShell
Let’s start by going through the fundamentals of the Get-UsageAggregates cmdlet. Using this cmdlet, you may retrieve data on Azure resource utilization by date range, metrics such as hourly or daily usage, and more. The primary cmdlet that does the most of the magic you’ll learn in this tutorial is Get-UsageAggregates.
Let’s imagine you need to obtain Azure resource usage from April 1st, 2017, to September 12th, 2022, to illustrate the Get-UsageAggregates cmdlet. Any date range can be utilized using the same methodology.
$params = @{
ReportedStartTime = '04-01-17'
ReportedEndTime = '09-12-22'
AggregationGranularity = 'Hourly'
ShowDetails = $true
}
$usageData = Get-UsageAggregates @params
Example To Build an Azure Usage Report with PowerShell
I can help you create a PowerShell script to easily query Azure resource usage. Here’s an example script that uses the Azure PowerShell module to retrieve resource usage information:
# Install the Azure PowerShell module if not already installed
# Install-Module -Name Az
# Import the Azure PowerShell module
Import-Module -Name Az
# Connect to your Azure account
Connect-AzAccount
# Set the subscription context
Set-AzContext -SubscriptionId "your_subscription_id"
# Get the resource usage for the specified time range
$startDate = Get-Date "2023-07-01"
$endDate = Get-Date "2023-07-08"
# Get the resource usage data
$usageData = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $endDate
# Display the resource usage information
$usageData | Format-Table -Property UsageStart, UsageEnd, MeterId, MeterName, Quantity, Unit, Cost
# Disconnect from the Azure account
Disconnect-AzAccount
Before running the script, make sure you have the Azure PowerShell module installed. If it’s not installed, you can uncomment the Install-Module line to install it.
To use the script, you’ll need to replace “your_subscription_id” with your actual Azure subscription ID. You can find your subscription ID in the Azure portal.
The script retrieves resource usage data for a specific time range, specified by the $startDate and $endDate variables. You can modify these variables to specify the desired time range.
The script uses the Get-AzConsumptionUsageDetail cmdlet to retrieve the resource usage details. It then formats and displays the retrieved data using the Format-Table cmdlet.
Remember to connect to your Azure account using Connect-AzAccount before running the script and disconnect using Disconnect-AzAccount afterward.
Make sure to run the script in a PowerShell environment with the Azure PowerShell module installed and authenticated with appropriate permissions to access the resource usage information in your Azure subscription.