Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
Smamwise
Frequent Visitor

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 1
Anonymous
Not applicable

Hi  @Smamwise ,

 

You can refer to the following documentation and see if it helps you to "create a list or an array and loop those in the foreach bit".

azure - How do I properly use ForEach() statement of List? - Stack Overflow

adamtheautomator.com

PowerShell array initialization - Stack Overflow

morgantechspace.com

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.