Forum Discussion
Alex_Roughag
2 years agoRegular Visitor
Microsoft 365 Usage Analytics refresh dataset with Service Principal
I added "Microsoft 365 Usage Analytics" as a workspace using admin privileges. However, I want to automate refresh with the Service Principal. So I created a script, a service principal (which I assi...
Alex_Roughag
2 years agoRegular Visitor
Hi Anonymous
Thank you for replying back.
1. Yes. The service principal has a Contributor role on the workspace.
2. As per the powershell script below, an access token is obtained using oAuth2.
3. I'm using the following powershell script to refresh the dataset. Not sure if it's possible to change auth to refresh token. Maybe you can advise?
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$clientid = "<clientID>"
$NameSecret = "<secretName>"
$scope = "https://analysis.windows.net/powerbi/api/.default"
$tenantid = "<tenantID>"
$groupID = “<groupID>”
$dataset = “<datasetID>"
$myCredential = Get-AutomationPSCredential -Name $NameSecret
$clientsecret = $myCredential.GetNetworkCredential().Password
# Getting Access Token
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/x-www-form-urlencoded")
$body = "client_id=$clientid&client_secret=$clientsecret&scope=$scope&grant_type=client_credentials"
$response = Invoke-RestMethod "https://login.microsoftonline.com/$tenantid/oauth2/v2.0/token" -Method 'POST' -Headers $headers -Body $body
$token = $response.access_token
$token = "Bearer $token"
# Refresh the Data Set
$authHeader = @{
'Content-Type'='application/json'
'Authorization'= $token
}
$groupsPath = ""
if ($groupID -eq "me") {
$groupsPath = "myorg"
} else {
$groupsPath = "myorg/groups/$groupID"
}
$uri = "https://api.powerbi.com/v1.0/$groupsPath/datasets/$dataset/refreshes"
Invoke-RestMethod -Uri $uri –Headers $authHeader –Method POST
$statusURL = "https://api.powerbi.com/v1.0/$groupsPath/datasets/$dataset/refreshes"
$response = Invoke-RestMethod $statusURL -Method 'GET' -Headers $authHeader
$status = $response.value[0].status
lbendlin
2 years agoSuper User
Have you considered using Login-PowerBIServiceAccount or AAD/Entra auth?