Using the Azure Billing API with Powershell

2015, May 18    

Once you’ve got an Enterprise Agreement with Microsoft Azure, you’ll probably want to know how much you’re spending.

As with everything Azure, you’re better off forgetting the UI and using Powershell.

Unfortunately, the billing API doesn’t return detailed information in a Json or xml format – it is a long string delimited by commas and linebreaks.

Before we can do anything with the detailed data we get from the API we need to clean it up into a powershell object.  Once we have a usable object to work with then we can start playing with the billing data.

In the example below, I’ve used the sample data account (Enrollment #100), just plug in your own enrollment number and the access key from the Azure Enterprise Site and you’ll be away.
For full documentation on the EA API, you can find the API guide here

$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"
Azure Cost for 05/17/2015 = £75.58
Azure Cost for 2015-05 = £2287.34