Running the Power BI Refresh API’s Headless


 

Was talking to one of our Power BI MVPs, Phil Seamark and he mentioned needing to run adhoc refreshes from an SSRS job….but the script needed to be headless.

That being the case i modified Sirui’s sample to do just that!

Note Live Writer has almost certainly broken the quotes but all you need is the two lines below.

 

$UserCred = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" -ArgumentList "chass@microsoft.com", "MyPassword”

$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $UserCred)

 

# ****************************************************************************

# This sample script calls the Power BI API to programmatically trigger a refresh for the dataset

# It then calls the Power BI API to programmatically to get the refresh history for that dataset

# For full documentation on the REST APIs, see:

# https://msdn.microsoft.com/en-us/library/mt203551.aspx

# Instructions:

# 0. Install PowerShell

# This sample script calls the Power BI API to programmatically trigger a refresh for the dataset

# It then calls the Power BI API to programmatically to the dataset can be

# updated successfully

# 2. Fill in the parameters below

# 3. Run the PowerShell script

# Parameters - fill these in before running the script!

# =====================================================

# An easy way to get group and dataset ID is to go to dataset settings and click on the dataset

# that you'd like to refresh. Once you do, the URL in the address bar will show the group ID and

# dataset ID, in the format:

# app.powerbi.com/groups/{groupID}/settings/datasets/{datasetID}

$groupID = "003ec613-3225-4c79-bd03-5920b0e1787f" # the ID of the group that hosts the dataset. Use "me" if this is your My Workspace

$datasetID = "312873ce-0cb2-48c0-93a6-6ec2b2d6eed7" # the ID of the dataset that hosts the dataset

# AAD Client ID

# To get this, go to the following page and follow the steps to provision an app

# https://dev.powerbi.com/apps

# To get the sample to work, ensure that you have the following fields:

# App Type: Native app

# Redirect URL: urn:ietf:wg:oauth:2.0:oob

#  Level of access: all dataset APIs

$clientId = "5961365f-313a-4308-ac7d-4c4126e24684"

# End Parameters =======================================

# Calls the Active Directory Authentication Library (ADAL) to authenticate against AAD

function GetAuthToken

{

$adal = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"

$adalforms = "${env:ProgramFiles(x86)}\Microsoft SDKs\Azure\PowerShell\ServiceManagement\Azure\Services\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"

[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null

[System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null

$redirectUri = "urn:ietf:wg:oauth:2.0:oob"

$resourceAppIdURI = "https://analysis.windows.net/powerbi/api"

$authority = "https://login.windows.net/common/oauth2/authorize";

$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority

$UserCred = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" -ArgumentList "chass@microsoft.com", "MyPassword”

$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $UserCred)

return $authResult

}

# Get the auth token from AAD

$token = GetAuthToken

# Building Rest API header with authorization token

$authHeader = @{

'Content-Type'='application/json'

'Authorization'=$token.CreateAuthorizationHeader()

}

# properly format groups path

$groupsPath = ""

if ($groupID -eq "me") {

$groupsPath = "myorg"

} else {

$groupsPath = "myorg/groups/$groupID"

}

# Refresh the dataset

$uri = "https://api.powerbi.com/v1.0/$groupsPath/datasets/$datasetID/refreshes"

Invoke-RestMethod -Uri $uri –Headers $authHeader –Method POST –Verbose

# Check the refresh history

$uri = "https://api.powerbi.com/v1.0/$groupsPath/datasets/$datasetID/refreshes"

Invoke-RestMethod -Uri $uri –Headers $authHeader –Method GET –Verbose


Comments (0)

Skip to main content