Azure offers a Utilization API that permits you to retrieve detailed utilization data in your Azure assets. This data will be worthwhile for monitoring and optimizing your useful resource consumption, monitoring prices, and understanding the utilization patterns of your Azure companies.
To entry the Azure utilization report utilizing PowerShell, you may make the most of the Azure PowerShell module (Az). This module offers cmdlets that permit you to work together with numerous Azure companies, together with retrieving utilization particulars.
Methods to Obtain Utilization Report With Azure Account Middle
The Azure Account Middle gives quite a lot of person knowledge that you could obtain. Chances are you’ll examine the historical past of your billing cycles, acquire invoices, and get an outline of your Azure membership by visiting the Azure Account Middle, a web site.
- The date vary can’t be altered in any manner. As a result of the dates are set to correspond along with your fee cycle, you can not limit utilization to a sure timeframe.
- There’s a ton of extra data in use studies that you simply may not want.
- Maybe you require utilization statistics over the earlier 60 days.
To cowl all your wanted dates, you would want to obtain utilization information for a minimum of three billing cycles. - You’ll then must take away the additional knowledge as soon as the studies had been downloaded. The subsequent step could be to plot a technique for compiling such knowledge.
- You possibly can create a reusable script, operate, or module in PowerShell to drag Azure useful resource consumption studies. These can then be executed manually utilizing a number of strains of code or routinely as a scheduled process.
First Get Azure Useful resource Utilization Knowledge Utilizing PowerShell
Let’s begin by going by way of the basics of the Get-UsageAggregates cmdlet. Utilizing this cmdlet, you might retrieve knowledge on Azure useful resource utilization by date vary, metrics comparable to hourly or each day utilization, and extra. The first cmdlet that does the many of the magic you’ll study on this tutorial is Get-UsageAggregates.
Let’s think about you have to acquire Azure useful resource utilization from April 1st, 2017, to September twelfth, 2022, for instance the Get-UsageAggregates cmdlet. Any date vary will be utilized utilizing the identical methodology.
$params = @{
ReportedStartTime="04-01-17"
ReportedEndTime="09-12-22"
AggregationGranularity = 'Hourly'
ShowDetails = $true
}
$usageData = Get-UsageAggregates @params
Instance To Construct an Azure Utilization Report with PowerShell
I may help you create a PowerShell script to simply question Azure useful resource utilization. Right here’s an instance script that makes use of the Azure PowerShell module to retrieve useful resource utilization data:
# Set up the Azure PowerShell module if not already put in
# Set up-Module -Identify Az
# Import the Azure PowerShell module
Import-Module -Identify Az
# Connect with your Azure account
Join-AzAccount
# Set the subscription context
Set-AzContext -SubscriptionId "your_subscription_id"
# Get the useful resource utilization for the required time vary
$startDate = Get-Date "2023-07-01"
$endDate = Get-Date "2023-07-08"
# Get the useful resource utilization knowledge
$usageData = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $endDate
# Show the useful resource utilization data
$usageData | Format-Desk -Property UsageStart, UsageEnd, MeterId, MeterName, Amount, Unit, Value
# Disconnect from the Azure account
Disconnect-AzAccount
Earlier than operating the script, be sure you have the Azure PowerShell module put in. If it’s not put in, you may uncomment the Set up-Module line to put in it.
To make use of the script, you’ll want to interchange “your_subscription_id” along with your precise Azure subscription ID. You will discover your subscription ID within the Azure portal.
The script retrieves useful resource utilization knowledge for a selected time vary, specified by the $startDate and $endDate variables. You possibly can modify these variables to specify the specified time vary.
The script makes use of the Get-AzConsumptionUsageDetail cmdlet to retrieve the useful resource utilization particulars. It then codecs and shows the retrieved knowledge utilizing the Format-Desk cmdlet.
Keep in mind to connect with your Azure account utilizing Join-AzAccount earlier than operating the script and disconnect utilizing Disconnect-AzAccount afterward.
Be sure that to run the script in a PowerShell atmosphere with the Azure PowerShell module put in and authenticated with acceptable permissions to entry the useful resource utilization data in your Azure subscription.