Forum Discussion

Smamwise's avatar
Smamwise
Frequent Visitor
2 years ago

Refresh reports with refresh schedules enabled via APIs and Powershell

Hi!

 

I am trying to write a Powershell script that would refresh all reports that have refresh schedules enabled.

 

The refresh api call should have a condition to only use dataset ids with refresh enabled. Currently refreshes all datasets.

The API call foreach code block should work like this: Loop datasetIds which have refresh schedule enable = true.

 

I might need to store the dataset ids with schedule enabled to a list or an array and loop those in the foreach bit to do this, but I am not sure how.

 

Here is my script. The part that requires input is the last one. Commented # POST Refresh datasets

 

# Parameters

$clientId = "xxxx-xxxx-xxx"
$tenantId = "xxxx-xxxx-xxx"
$clientSecret = "xxxx-xxxx-xxx"
$groupID = "xxxx-xxxx-xxx"

# End Parameters

# POST AuthToken

$body = @{
    "grant_type" = "client_credentials"
    "client_id" = $clientId
    "client_secret" = $clientSecret
    "scope" = "https://analysis.windows.net/powerbi/api/.default"
    }

$authUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$authResponse = Invoke-RestMethod -Uri $authUrl –Method POST -Body $body
$headers = @{
             "Content-Type" = "application/json";
             "Authorization" = $authResponse.token_type + " " +
                                $authResponse.access_token
           }

# GET group id for URI

$groupsPath = ""
if ($groupID -eq "me") {
    $groupsPath = "myorg"
} else {
    $groupsPath = "myorg/groups/$groupID"
}

$uril = "https://api.powerbi.com/v1.0/$groupsPath"
$restResponse1 = Invoke-RestMethod -Uri $uril -Method GET -Headers $headers

# GET dataset ids

$x = $restResponse1.id
    $groupID = $x.Id #workspace Id
    $uri = "https://api.powerbi.com/v1.0/$groupsPath/$groupID/datasets"
    $GETdatasetID = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers
    $d = $GETdatasetID.value
    
# GET Refresh schedules

    foreach($j in $d) {
        $datasetID=$j.Id #dataset Id
        $uri = "https://api.powerbi.com/v1.0/$groupsPath/$groupID/datasets/$datasetID/refreshSchedule"
        $GETrefreshSchedule = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers
    }
    
# POST Refresh datasets
# This should have a condition to only use dataset ids with refresh enabled: $GETrefreshSchedule.enabled = "True"
# Currently refreshes all datasets

    foreach($j in $d) {
        $datasetID=$j.Id #dataset Id
        $uri = "https://api.powerbi.com/v1.0/$groupsPath/$groupID/datasets/$datasetID/refreshes"
        $POSTrefreshDataset = Invoke-RestMethod -Uri $uri -Method POST -Headers $headers
    }

 

 

1 Reply