Power BI dataset refresh using Powershell

I've been using this powershell script to refresh the dataset in Power BI which was worked for me, but the problem was when I ran the script there was always a pop-up for me to login to my Power BI account. I really want to know if there's anyway to make the script auto login for me? Thank You

script: https://github.com/Azure-Samples/powerbi-powershell/blob/master/manageRefresh.ps1

Upvotes: 0

Views: 4349

Answers (1)

R. Conijn
R. Conijn

Reputation: 26

With the following you can easily get access the group info:

$pbiUsername = "< USERNAME >"
$pbiPassword = "< PASSWORD >"
$clientId = "< CLIENT-ID >"

$body = @{"resource" = "https://analysis.windows.net/powerbi/api";
    "client_id" = $clientId;
    "grant_type" = "password";
    "username" = $pbiUsername;
    "password" = $pbiPassword;
    "scope" = "openid"
    }

$authUrl = "https://login.windows.net/common/oauth2/token/"
$authResponse = Invoke-RestMethod -Uri $authUrl –Method POST -Body $body

$headers = @{
             "Content-Type" = "application/json";
             "Authorization" = $authResponse.token_type + " " + 
                                $authResponse.access_token
           }

$restURL = "https://api.powerbi.com/v1.0/myorg/groups"
$restResponse = Invoke-RestMethod -Uri $restURL –Method GET -Headers $headers

Here the clientId is the Id from the 'native' AAD client created using https://dev.powerbi.com/apps

To send a refresh command one uses the Urls:

If the dataset is in your own workspace:

$datasetId = "DATASET-ID"
$restUrl = "https://api.powerbi.com/v1.0/myorg/datasets/$datasetId/refreshes"

Otherwise, when it is in a group workspace:

$datasetId = "DATASET-ID"
$groupId = "GROUP-ID"
$restUrl = 
 "https://api.powerbi.com/v1.0/myorg/groups/$groupId/datasets/$datasetId/refreshes"

Now send the job to the powerBI Server:

$body = @{
            "notifyOption"= "MailOnFailure"
         }

$restResponse = Invoke-RestMethod -Uri $restUrl –Method POST -Headers $headers -Body $body

See also the blog post: https://blog.gbrueckl.at/2017/08/refresh-powerbi-datasets-powershell-azure-runbooks/

Upvotes: 0

Related Questions