As we all know that Power BI supports multiple REST APIs which helps us to automate certain operations like export the Power BI objects for inventory, export activity log for auditing, export and import reports into different workspace, add users into workspace and much more. Those APIs can be called from tools like PowerShell and tool which support REST API call.
I am more interested to call the direct REST API using PowerShell scripts rather than using Power BI PowerShell cmdlets.
Recently I got question from one of the Power BI users who an admin for one of the Power BI workspaces and he wants to get the list of objects which are available in that workspace. As there are more number of reports and datasets and it was difficult for him to manage etc.
This scenario is something for individual users who wants to leverage the REST API for their activities.
Let us focus on the above scenario and see what the options are available to help this scenario.
We have below options
- Power BI REST API site
- Call the REST APIs from PowerShell
Power BI REST API site
One of the best features which we have in Power BI REST document is, you can try the API directly on the browser page and you don’t need anything else. Just sign in with your Power BI account which will create the temporary Azure AD token and use the same to extract any information from Power BI.
REST API Url – https://docs.microsoft.com/en-us/rest/api/power-bi/
Example – if you need to get the list of reports from Power BI workspace then choose “Reports – Get Reports in Group” API and you will see like below.
You can see Try it option which will be available for almost all APIs. Click the Try it button and it will ask you to sign in with your Power BI credentials. You will get below page,
You can pass the group id which is workspace id and click run below and it will show all the reports from your workspace as a response in JSON format.
Some of the REST API need to pass the body parameter which you can get it from samples that is available on each REST API.
Call the REST API from PowerShell
To call the REST API from PowerShell, you need to generate the Azure AD token which is a first prerequisite. To get the token you need to use username and password or service principal using client id and secret. To get the client ID, you need to register an APP in Azure AD and get the Power BI permissions.
This will be difficult for Power BI user who just maintain some workspaces. Sometimes, organization put restrictions to use the client ID only for admin activities by administrators.
The below approach will help to avoid using client id to run any Power BI REST API but still using REST API. We have Power BI PowerShell cmdlets which will help in this scenario.
Most of the operations can be performed directly on PowerShell cmdlets but still I prefer the dedicated REST API as we will get more control.
Login-PowerBIServiceAccount
#Get the token
$token = Get-PowerBIAccessToken -AsString
$auth_header = @{
'Content-Type'='application/json'
'Authorization'=$token
}
$uri = "https://api.powerbi.com/v1.0/myorg/groups/e2fa6274-659a-4f64-8d47-9ac5636dfeb3/reports"
$all_reports = (Invoke-RestMethod -Uri $uri –Headers $auth_header –Method GET).value
$all_reports.name
In the above line of scripts, We are using Login-PowerBiServiceAccount PowerShell cmdlets to login with Power BI service and using Get-PowerBIAccessToken, we can get the token which can be used on following REST API calls.
Note – This approach is not recommended for automation as you need to sign-in every time manually.
Leave a Reply