forked from robotechredmond/Azure-PowerShell-Snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Azure Billing API v2.ps1
84 lines (57 loc) · 1.95 KB
/
Azure Billing API v2.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Set date range for exported usage data
$reportedStartTime = "2017-03-07"
$reportedEndTime = "2017-03-08"
# Authenticate to Azure
Login-AzureRmAccount
# Select an Azure Subscription for which to report usage data
$subscriptionId =
(Get-AzureRmSubscription |
Out-GridView `
-Title "Select an Azure Subscription ..." `
-PassThru).SubscriptionId
Select-AzureRmSubscription -SubscriptionId $subscriptionId
# Set path to exported CSV file
$filename = ".\usageData-${subscriptionId}-${reportedStartTime}-${reportedEndTime}.csv"
# Set usage parameters
$granularity = "Daily" # Can be Hourly or Daily
$showDetails = $true
# Export Usage to CSV
$appendFile = $false
$continuationToken = ""
$usageData = Get-UsageAggregates `
-ReportedStartTime $reportedStartTime `
-ReportedEndTime $reportedEndTime `
-AggregationGranularity $granularity `
-ShowDetails:$showDetails
Do {
$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.ContinuationToken) {
$continuationToken = $usageData.ContinuationToken
$usageData = Get-UsageAggregates `
-ReportedStartTime $reportedStartTime `
-ReportedEndTime $reportedEndTime `
-AggregationGranularity $granularity `
-ShowDetails:$showDetails `
-ContinuationToken $continuationToken
} else {
$continuationToken = ""
}
$appendFile = $true
} until (!$continuationToken)