Azure invoices are now available via API!

Today I want to talk about Azure invoices.
Till now Azure invoices was available to download only from Azure Management Portal, but since last week it was changed. Now user which has proper access to subscription (Billing Reader and proper administrative access) can download Azure invoices programatically. However there are some limitations. API does not allows to download invoices for following subscription types:
• Enterprise agreements
• Support
• Azure in open

Azure API allows to download not only the newest invoices, but also those generated in the past.
Ok, but you will ask how to do it? Below instruction will help you 🙂
First of all login to Azure via Powershell (if you don’t have AzureRM module this link should help you)

 Login-AzureRmAccount

Next gather all subscription for which your account have an access

Get-AzureRMSubscription

Select subscription ID for which you want to gather invoices and use it in subscription context

Set-AzureRmContext -SubscriptionId <subscription Id>

Once the context is set use below command to gather invoice information for last month.

$InvoiceDetails = Get-AzureRmBillingInvoice -Latest

Above part can be adjusted for your needs, for instance you can choose to download invoice for few months back.
If you will display $InvoiceDetails variable you will see DownloadURI attribute which should looks simlar to below:
https://{billingstorage}.blob.core.windows.net/invoices/{invoice identifier}.pdf?sv=2014-02-14&sr=b&sig=XlW87Ii7A5MhwQVvN1kMa0AR79iGiw72RGzQTT% 2Fh4YI%3D&se=2017-04-06T23%3A25%3A56Z&sp=r
Under this link you can find your invoice in .pdf format.
The only thing we need to do right now is to download file using Powershell.

Invoke-WebRequest -Uri $InvoiceDetails.DownloadUrl -OutFile &lt;directory&gt;\Invoice.pdf

You can add to script some additional steps like for example sending invoice to your accountant.
It can really automate task which should be performed every month 🙂

Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.