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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

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
July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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