Perhaps not surprisingly, one of my more popular post this year was Power BI REST API which I wrote exactly twelve months ago. That was originally written in C# but I also did a follow up post that was written in Python. I guess the relative popularity of these posts stems from the enormous and growing popularity of Power BI and REST APIs in general.
Because I’ve been using more and more PowerShell this year and this is now becoming close to my favourite language (after C#), I’d thought I’d do another follow up, this time, in PowerShell. As with the earlier posts, this will focus just on the code so you’ll need to do all the background requirements like opening and registering an account with Power BI and Azure etc-you can find instruction here. Don’t worry about the tokens bit, we’ll be doing it here in PowerShell
As for the code, like most language, there are several ways to do it. You could as most do, just download and import the various modules from GitHub and use it. I personally prefer not to when I can and on this occasion especially, because you need admin rights to install the necessary modules and I happen to be on a machine without that right. Speaking about permissions, pay particular attention to the first two lines. This is to get over any corporate firewall proxy issues you may encounter (and can be removed if not needed).
Probably also worth noting I’ve done it using a script based approach. I point this out as with the release of version 5, PowerShell has truly become object oriented and we could have completely written it that way had we wish.
As with C# and Python, the code it’s pretty straightforward and very similar as seen below (it is REST after all). Simply ‘get’ a token and then perform an action, in this example, get a dataset (actually Get Datasets, plural). You can see all the available actions you can perform from the official documentations page here. The key for most is authentication and knowing how to get the token as we have shown and without having to install additional modules.
Once you have the token, it’s just a matter of performing your actual request, in this instance, getting the datasets (plural).