Q. Can I extract usage information from Azure subscriptions using PowerShell?
A. The Azure module contains a number of cmdlets to fetch usage data, specifically Get-UsageAggregates and Get-UsageMetrics. Below is an example of fetching the usage data. Change the dates and subscription ID.
$reportStartTime = "2015-08-01"
$reportEndTime = "2015-08-31"
$filename = ".\usageData-$reportStartTime-$reportEndTime.csv"
$granularity = "Daily" # Also could be Hourly
$showDetails = $true
$appendFile = $false
$subscriptionID = '466c1a5d-e93b-4138-91a5-670daf44b0f8'
$continuationToken = ""
Do {
$usageData = Get-UsageAggregates `
-ReportedStartTime $reportStartTime `
-ReportedEndTime $reportEndTime `
-AggregationGranularity $granularity `
-ShowDetails:$showDetails `
-ContinuationToken $continuationToken
$usageData.UsageAggregations.Properties |
Select-Object `
UsageStartTime, `
UsageEndTime, `
@{n='SubscriptionId';e={$subscriptionId}}, `
MeterCategory, `
MeterId, `
MeterName, `
MeterSubCategory, `
MeterRegion, `
Unit, `
Quantity, `
@{n='Project';e={$_.InfoFields.Project}}, `
InstanceData |
Export-Csv `
-Append:$appendFile `
-NoTypeInformation:$true `
-Path $filename
if ($usageData.NextLink)
{
$continuationToken = `
[System.Web.HttpUtility]::`
UrlDecode($usageData.NextLink.Split("=")[-1])
}
else
{
$continuationToken = ""
}
$appendFile = $true
} until (!$continuationToken)
Once executed open the created CSV which shows all the usage data.
1 comment
Hide comments