Search code examples
powershellrestpowerbipowerbi-api

Getting dataset refreshes as admin through Power BI REST API with PowerShell


I have a Power BI workspace with GUID [$group] and in it there is a dataset with GUID [$dataset]. From the Azure Cloud Shell I can run the following code:

Connect-PowerBIServiceAccount
# I log in with my personal credentials
$Url = 'https://api.powerbi.com/v1.0/myorg/groups/' + $group + '/datasets/' + $dataset + '/refreshes?$top=3'
$results = $(Invoke-PowerBIRestMethod -Url $Url -Method Get | ConvertFrom-Json).value

This works fine and I get the last 3 refresh events, but I would like to modify the previous code so that it does not only work for my workspaces, but for all the workspaces of the tenant. We have a system in place so that I can get the required credentials and then connect through a service principal as follows:

Connect-PowerBIServiceAccount -ServicePrincipal -Credential $Credentials -Tenant $TenantId

Now that I am connected through the service principal, if I try to run again

$Url = 'https://api.powerbi.com/v1.0/myorg/groups/' + $group + '/datasets/' + $dataset + '/refreshes?$top=3'
$results = $(Invoke-PowerBIRestMethod -Url $Url -Method Get | ConvertFrom-Json).value

I get the error message:

Invoke-PowerBIRestMethod: One or more errors occurred. ({
  "code": "ItemNotFound",
  "message": "Couldn't find dataset id "
})

I have tried to identify the problem going down from groups to datasets to refreshes. I have found that the string '/admin' is required in the url for this to work, and in fact if I run

$Url = 'https://api.powerbi.com/v1.0/myorg/admin/groups/'+$group+'/datasets'
$results_datasets = $(Invoke-PowerBIRestMethod -Url $Url -Method Get | ConvertFrom-Json).value

I get the datasets of my workspace. However, I cannot go "further down" than datasets! Let me explain. I can check that

$dataset -in $results_datasets.id
# True

But when I try to replace the url with one of the following

$Url = 'https://api.powerbi.com/v1.0/myorg/admin/groups/' + $group + '/datasets/' + $dataset
$Url = 'https://api.powerbi.com/v1.0/myorg/admin/groups/' + $group + '/datasets/' + $dataset + '/refreshes'
$Url = 'https://api.powerbi.com/v1.0/myorg/admin/groups/' + $group + '/datasets/' + $dataset + '/refreshes?$top=3'

I always get the error message

Invoke-PowerBIRestMethod: One or more errors occurred. ({
  "code": "",
  "message": "No HTTP resource was found that matches the request URI 'http://wabi-europe-north-b-redirect.analysis.windows.net/v1.0/myorg/admin/groups/[$group]/datasets/[$dataset etc]'."
})

Why does this work until datasets and stops working further down? Is there a better way to get the refreshes data through a service principal?

P.S.: both 'Allow service principals to use Power BI APIs' and 'Dataset Execute Queries REST API' are enabled for the security group of the service principal.


Solution

  • As far as I can see there is no admin API returning refreshes for dedicated dataset in given group.

    You an try this one, but this is NOT admin one. https://api.powerbi.com/v1.0/myorg/groups/{groupId}/datasets/{datasetId}/refreshes

    Please see more details under this link: https://learn.microsoft.com/en-us/rest/api/power-bi/datasets/get-refresh-history-in-group

    You can also read this discussion where you can find similar case like your one. https://community.fabric.microsoft.com/t5/Service/Unable-to-retrieve-dataset-refresh-history/m-p/2820487