Azure EA Subscription - Billing Report

707 views Asked by At

Trying to fetch azure billing reports via powershell, before getting started it asks for the enrollment number and access key, enrollment key can be found from https://ea.azure.com, however access key is not explicitly mentioned any where, if it can be obtained from storage account then i have more than 20 storage account for different resources,

Any suggestion who has worked on to retrieve billing EA details via powershell.

1

There are 1 answers

0
Ranadip Dutta On

I would like you to go through Gordon Byers

Sample:

$baseurl = "https://ea.azure.com/rest/"
$enrollmentNo ="100"
$accesskey ="eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6Il92ZHZieDJYRUkyb2l5N09abVpVelZGelM2TSJ9.eyJpc3MiOiJlYS5taWNyb3NvZnRhenVyZS5jb20iLCJhdWQiOiJjbGllbnQuZWEubWljcm9zb2Z0YXp1cmUuY29tIiwibmJmIjoxNDI4NDYwOTgxLCJleHAiOjE0NDQyNzIxODEsIkVucm9sbG1lbnROdW1iZXIiOiIxMDAiLCJJZCI6Ijk3OGQ5MGRjLTEyZjUtNDVmZC1hZWY2LWNiZTQ0ZDE0NmFmNiIsIlJlcG9ydFZpZXciOiJTeXN0ZW0iLCJQYXJ0bmVySWQiOiIifQ.QEHdbHmUz4TXO8Ljhn8ktnt5m7rHO0hsRrDLTvoQP4luL-8gy98CywFi2OByhfPL6UY5Vx8MYtwTQhMbzefS6Cd8hNopP74AL1ENLT-WGn42JG2mWesizIuN1QHV-obZR5rKDT57leurgEWy3LscWlwM0v5vhtk-9e5p8_9YgYqnF3rC05l1kUBQYzEIeYeMxcp8YxsqlnQNoEdCIA4UN08Py7zjr0ohCYbkm76a0-XdquqOTdSKGrnFkmwPxzjWMkuX3f2zpck_Ps8x8TVBpfvywly0QjUbN0ssmLV20zrS8FqkKSdx13uQVzM3MXCBylu5WvhTKE_ogRJdxPpfiA"
$authHeaders = @{"authorization"="bearer $accesskey";"api-version"="1.0"}

Write-Host "Summary info"
$url= $baseurl + $enrollmentNo + "/usage-reports"
$sResponse = Invoke-WebRequest $url -Headers $authHeaders
$sContent = $sResponse.Content | ConvertFrom-Json 
#$sContent.AvailableMonths

Write-Host "Detailed Month info"
#$month="2015-04"
$month=$sContent.AvailableMonths[0].Month
$url= $baseurl + $enrollmentNo + "/usage-report?month=$month&type=detail"
$mResponse = Invoke-WebRequest $url -Headers $authHeaders

#Split the response up into an array from a string
$mContent = ($mResponse.Content -split '[rn]')

#Convert from CSV to object
$mCsv = $mContent | Where-Object { [regex]::matches($_,",").count -gt 28} | ConvertFrom-Csv
#$mCsv = $mcontent[4..$($mcontent.count)] | ConvertFrom-Csv

#$mCsv | Select-Object ServiceResource, Service, Product, date, ResourceQtyConsumed, ResourceRate, ExtendedCost  | ft

$filterDay =$mCsv[-1].Date
Write-Host "Examining for single day $filterDay"
$aday = $mCsv | Where-Object {$_.date -eq $filterDay}
$aday | Select-Object  Product, date, ResourceQtyConsumed, ResourceRate, ExtendedCost | Sort-Object ExtendedCost -Descending | ft

$adayCost = [math]::round($($aday | Select-Object -ExpandProperty ExtendedCost | Measure-Object -Sum).sum,2)
Write-Host "Azure Cost for $filterDay = £$adayCost" 

$monthCost = [math]::round($($mCsv | Select-Object -ExpandProperty ExtendedCost | Measure-Object -Sum).sum,2)
Write-Host "Azure Cost for $month = £$monthCost"

Note: The API guide is also mentioned in the link given over there. This is one of the best way for Azure Billing API

Hope it helps.