Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
7 years ago

Code to refresh a dataset where prior refresh failed

Hi everyone,

 

we have a number of "production" datasets, and these seem to fail to refresh occasionally. Uncertain why - maybe network hiccup, PBI service issue, database being backed up on our side, etc.

 

Long and short, I'm trying to write a script that will loop through every dataset in our production workspaces, check whether a) the dataset is set up to refresh, and b) if so did the last refresh work properly. Unfortunately I'm not a PowerShell developer (at all...) so I'm hitting some issues.

 

The main piece of code I have is:

 

$AllDatasets =
ForEach ($workspace in $Workspaces)
    {
    ForEach ($dataset in (Get-PowerBIDataset -Scope Organization -WorkspaceId $workspace.Id))
        {

        # check if dataset is supposed to refresh
        $url = "https://api.powerbi.com/v1.0/myorg/groups/" + $workspace.Id + "/datasets/" + $dataset.ID + "/refreshSchedule"
        $refreshSched = $false
        $refreshSched = (ConvertFrom-Json (Invoke-PowerBIRestMethod -Url $url -Method Get)).enabled


        # check status of last refresh
        $url = "https://api.powerbi.com/v1.0/myorg/groups/" + $workspace.Id + "/datasets/" + $dataset.ID + '/refreshes?$top=1'
        #Write-Host $url
        $last_status = "n/a"
        $last_status = (ConvertFrom-Json (Invoke-PowerBIRestMethod -Url $url -Method Get)).value.status



        [pscustomobject]@{
            WorkspaceName = $workspace.Name
            WorkspaceID = $workspace.Id
            DatasetName = $dataset.Name
            DatasetID = $dataset.Id
            DatasetAuthor = $dataset.ConfiguredBy
            Refresh = $refreshSched
            LastStatus = $last_status
            }
        }
    }

This seems to work pretty well - but it throws errors. For example, the bit of code where it calls the "/refreshSchedule" fails if the dataset isn't set to be refreshed. Ditto for when it calls the last status $top=1 bit.

 

Is there a simple way for me to test whether a dataset is set up to be refreshed, other than calling the /refreshSchedule API and getting an error back? And/or is there a better way to handle the error? I thought I could use the "IsRefreshable" field when querying the dataset itself...but that seems to be set to TRUE even for datasets that aren't scheduled to refresh.

 

Thanks!

Scott

No Replies