Skip to content

Commit 6fee470

Browse files
Merge pull request #124 from pappleby64/Usage
Usage
2 parents ebc75d1 + 91ad436 commit 6fee470

2 files changed

Lines changed: 191 additions & 0 deletions

File tree

Usage/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
As a prerequisite, make sure that you installed the correct PowerShell modules and versions:
2+
3+
```powershell
4+
Install-Module -Name 'AzureRm.Bootstrapper' -Scope CurrentUser
5+
Install-AzureRmProfile -profile '2017-03-09-profile' -Force -Scope CurrentUser
6+
Install-Module -Name AzureStack -RequiredVersion 1.2.9 -Scope CurrentUser
7+
```
8+
```
9+
Use this script to extract usage data from the AzureStack Usage API's and export it to a CSV file
10+
For more information on Billing and Usage see [here](https://docs.microsoft.com/en-us/azure/azure-stack/azure-stack-billing-and-chargeback)
11+
```

Usage/Usagesummary.ps1

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
<#
2+
.Synopsis
3+
Exports usage meters from Azure Stack to a csv file
4+
.DESCRIPTION
5+
Long description
6+
.EXAMPLE
7+
Export-AzureStackUsageDetails -StartTime 2/15/2017 -EndTime 2/16/2017 -AzureStackDomain azurestack.local -AADDomain mydir.onmicrosoft.com -Granularity Hourly
8+
#>
9+
function Export-AzureStackUsage {
10+
Param
11+
(
12+
[Parameter(Mandatory = $true)]
13+
[datetime]
14+
$StartTime,
15+
[Parameter(Mandatory = $true)]
16+
[datetime]
17+
$EndTime ,
18+
[Parameter(Mandatory = $true)]
19+
[String]
20+
$AzureStackDomain ,
21+
[Parameter(Mandatory = $true)]
22+
[String]
23+
$AADDomain ,
24+
[Parameter(Mandatory = $false)]
25+
[ValidateSet("Hourly", "Daily")]
26+
[String]
27+
$Granularity = 'Hourly',
28+
[Parameter(Mandatory = $false)]
29+
[String]
30+
$CsvFile = "UsageSummary.csv",
31+
[Parameter (Mandatory = $false)]
32+
[PSCredential]
33+
$Credential,
34+
[Parameter(Mandatory = $false)]
35+
[Switch]
36+
$TenantUsage,
37+
[Parameter(Mandatory = $false)]
38+
[String]
39+
$Subscription,
40+
[Parameter(Mandatory = $false)]
41+
[Switch]
42+
$Force
43+
)
44+
45+
#Initialise result count and meter hashtable
46+
$Total = 0
47+
$meters = @{
48+
'F271A8A388C44D93956A063E1D2FA80B' = 'Static IP Address Usage'
49+
'9E2739BA86744796B465F64674B822BA' = 'Dynamic IP Address Usage'
50+
'B4438D5D-453B-4EE1-B42A-DC72E377F1E4' = 'TableCapacity'
51+
'B5C15376-6C94-4FDD-B655-1A69D138ACA3' = 'PageBlobCapacity'
52+
'B03C6AE7-B080-4BFA-84A3-22C800F315C6' = 'QueueCapacity'
53+
'09F8879E-87E9-4305-A572-4B7BE209F857' = 'BlockBlobCapacity'
54+
'B9FF3CD0-28AA-4762-84BB-FF8FBAEA6A90' = 'TableTransactions'
55+
'50A1AEAF-8ECA-48A0-8973-A5B3077FEE0D' = 'TableDataTransIn'
56+
'1B8C1DEC-EE42-414B-AA36-6229CF199370' = 'TableDataTransOut'
57+
'43DAF82B-4618-444A-B994-40C23F7CD438' = 'BlobTransactions'
58+
'9764F92C-E44A-498E-8DC1-AAD66587A810' = 'BlobDataTransIn'
59+
'3023FEF4-ECA5-4D7B-87B3-CFBC061931E8' = 'BlobDataTransOut'
60+
'EB43DD12-1AA6-4C4B-872C-FAF15A6785EA' = 'QueueTransactions'
61+
'E518E809-E369-4A45-9274-2017B29FFF25' = 'QueueDataTransIn'
62+
'DD0A10BA-A5D6-4CB6-88C0-7D585CEF9FC2' = 'QueueDataTransOut'
63+
'FAB6EB84-500B-4A09-A8CA-7358F8BBAEA5' = 'Base VM Size Hours'
64+
'9CD92D4C-BAFD-4492-B278-BEDC2DE8232A' = 'Windows VM Size Hours'
65+
'6DAB500F-A4FD-49C4-956D-229BB9C8C793' = 'VM size hours'
66+
}
67+
68+
#Output Files
69+
if (Test-Path -Path $CsvFile -ErrorAction SilentlyContinue) {
70+
if ($Force) {
71+
Remove-Item -Path $CsvFile -Force
72+
}
73+
else {
74+
Write-Host "$CsvFile alreday exists use -Force to overwrite"
75+
return
76+
}
77+
}
78+
New-Item -Path $CsvFile -ItemType File | Out-Null
79+
80+
#get auth metadata and acquire token for REST call
81+
$api = 'adminmanagement'
82+
if ($TenantUsage) {
83+
$api = 'management'
84+
}
85+
$uri = 'https://{0}.{1}/metadata/endpoints?api-version=1.0' -f $api, $AzureStackDomain
86+
$endpoints = (Invoke-RestMethod -Uri $uri -Method Get)
87+
$activeDirectoryServiceEndpointResourceId = $endpoints.authentication.audiences[0]
88+
$loginEndpoint = $endpoints.authentication.loginEndpoint
89+
$authority = $loginEndpoint + $AADDomain + '/'
90+
$powershellClientId = '0a7bdc5c-7b57-40be-9939-d4c5fc7cd417'
91+
92+
#region Auth
93+
if ($Credential) {
94+
$adminToken = Get-AzureStackToken `
95+
-Authority $authority `
96+
-Resource $activeDirectoryServiceEndpointResourceId `
97+
-AadTenantId $AADDomain `
98+
-ClientId $powershellClientId `
99+
-Credential $Credential
100+
}
101+
else {
102+
$adminToken = Get-AzureStackToken `
103+
-Authority $authority `
104+
-Resource $activeDirectoryServiceEndpointResourceId `
105+
-AadTenantId $AADDomain `
106+
-ClientId $powershellClientId
107+
}
108+
109+
if (!$adminToken) {
110+
Return
111+
}
112+
#endregion
113+
114+
#Setup REST call variables
115+
$headers = @{ Authorization = (('Bearer {0}' -f $adminToken)) }
116+
$armEndpoint = 'https://{0}.{1}' -f $api, $AzureStackDomain
117+
118+
if (!$Subscription) {
119+
#Get default subscription ID
120+
$uri = $armEndpoint + '/subscriptions?api-version=2015-01-01'
121+
$result = Invoke-RestMethod -Method GET -Uri $uri -Headers $headers
122+
$Subscription = $result.value[0].subscriptionId
123+
}
124+
125+
#build usage uri
126+
if (!$TenantUsage) {
127+
$uri = $armEndpoint + '/subscriptions/{0}/providers/Microsoft.Commerce/subscriberUsageAggregates?api-version=2015-06-01-preview&reportedstartTime={1:s}Z&reportedEndTime={2:s}Z&showDetails=true&aggregationGranularity={3}' -f $Subscription, $StartTime, $EndTime, $Granularity
128+
}
129+
else {
130+
$uri = $armEndpoint + '/subscriptions/{0}/providers/Microsoft.Commerce/UsageAggregates?api-version=2015-06-01-preview&reportedstartTime={1:s}Z&reportedEndTime={2:s}Z&showDetails=true&aggregationGranularity={3}' -f $Subscription, $StartTime, $EndTime, $Granularity
131+
}
132+
133+
Do {
134+
$result = Invoke-RestMethod -Method GET -Uri $uri -Headers $headers -ErrorVariable RestError -Verbose
135+
if ($RestError) {
136+
return
137+
}
138+
$usageSummary = @()
139+
$uri = $result.NextLink
140+
$count = $result.value.Count
141+
$Total += $count
142+
$result.value | ForEach-Object {
143+
$record = New-Object -TypeName System.Object
144+
$resourceInfo = ($_.Properties.InstanceData |ConvertFrom-Json).'Microsoft.Resources'
145+
$resourceText = $resourceInfo.resourceUri.Replace('\', '/')
146+
$subscription = $resourceText.Split('/')[2]
147+
$resourceType = $resourceText.Split('/')[7]
148+
$resourceName = $resourceText.Split('/')[8]
149+
#$record | Add-Member -Name Name -MemberType NoteProperty -Value $_.Name
150+
#$record | Add-Member -Name Type -MemberType NoteProperty -Value $_.Type
151+
$record | Add-Member -Name MeterId -MemberType NoteProperty -Value $_.Properties.MeterId
152+
if ($meters.ContainsKey($_.Properties.MeterId)) {
153+
$record | Add-Member -Name MeterName -MemberType NoteProperty -Value $meters[$_.Properties.MeterId]
154+
}
155+
$record | Add-Member -Name Quantity -MemberType NoteProperty -Value $_.Properties.Quantity
156+
$record | Add-Member -Name UsageStartTime -MemberType NoteProperty -Value $_.Properties.UsageStartTime
157+
$record | Add-Member -Name UsageEndTime -MemberType NoteProperty -Value $_.Properties.UsageEndTime
158+
$record | Add-Member -Name additionalInfo -MemberType NoteProperty -Value $resourceInfo.additionalInfo
159+
$record | Add-Member -Name location -MemberType NoteProperty -Value $resourceInfo.location
160+
$record | Add-Member -Name tags -MemberType NoteProperty -Value $resourceInfo.tags
161+
$record | Add-Member -Name subscription -MemberType NoteProperty -Value $subscription
162+
$record | Add-Member -Name resourceType -MemberType NoteProperty -Value $resourceType
163+
$record | Add-Member -Name resourceName -MemberType NoteProperty -Value $resourceName
164+
$record | Add-Member -Name resourceUri -MemberType NoteProperty -Value $resourceText
165+
$usageSummary += $record
166+
}
167+
$usageSummary | Export-Csv -Path $CsvFile -Append -NoTypeInformation
168+
if ($PSBoundParameters.ContainsKey(Debug)) {
169+
$result.value | Export-Csv -Path "$CsvFile.raw" -Append -NoTypeInformation
170+
}
171+
172+
}
173+
While ($count -ne 0)
174+
Write-Host "Complete - $Total Usage records written to $CsvFile"
175+
}
176+
177+
#Main
178+
179+
$aadCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList '<user@domain>', (ConvertTo-SecureString -String 'XXX' -AsPlainText -Force)
180+
Export-AzureStackUsage -StartTime 3/1/2017 -EndTime 3/13/2017 -AzureStackDomain 'local.azurestack.external' -AADDomain '<domain>' -Credential $aadCred -Granularity Hourly -Debug -Force

0 commit comments

Comments
 (0)