简体   繁体   中英

Azure Powershell automatic log-in to refresh Power BI

I'm currently developing a script to automatically refresh one of my Power BI datasets using Power BI's REST API. This script is a .ps1 file, called from my Python code as you can see below. I followed step-by-step from this tutorial, while my refresh.ps1 file was developed from this official MS source. There were just some minor adaptations to make it run, so if anyone wants to test it, I suggest using the code I'm posting at the end of this question.

However, besides both codes work well and I they allow me to refresh my dataset, every time they run I have to manually sign in in my Azure account through a prompted GUI . This makes this script useless to automate tasks, such as in my case. See the picture of the GUI below:

在此处输入图片说明

I did some resource, but couldn't find any similar case, in which Python was also being used. About Powershell, I read about using ADAL, but couldn't really get what ADAL is.

So far, my Python script is:

import subprocess, sys

def powershell(file):

    p = subprocess.Popen(["powershell.exe", file], stdout=sys.stdout)
    p.communicate()

MS.powershell(r"'C:\blablabla\refresh.ps1'")

While my refresh.ps1 file has the content shown below:

$groupID = "MY_GROUP_ID"
$datasetID = "MY_DATASET_ID"
$clientId = "MY_CLIENT_ID"

function GetAuthToken

{
       if(-not (Get-Module AzureRm.Profile)) {
         Import-Module AzureRm.Profile
       }

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

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

       $authority = "https://login.microsoftonline.com/common/oauth2/authorize";

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

       $authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")

       return $authResult
}

$token = GetAuthToken

$authHeader = @{
   'Content-Type'='application/json'
   'Authorization'=$token.CreateAuthorizationHeader()
}

$groupsPath = ""
if ($groupID -eq "me") {
    $groupsPath = "myorg"
} else {
    $groupsPath = "myorg/groups/$groupID"
}

$uri = "https://api.powerbi.com/v1.0/$groupsPath/datasets/$datasetID/refreshes"
Invoke-RestMethod -Uri $uri -Headers $authHeader -Method POST -Verbose

If you want to authenticate silently, you can use UserCredentials class, and pass user name and password to AcquireToken method. Your GetAuthToken function should become something like this:

function GetAuthToken
{
       # Change these
       $username = "chuck@norris.com"
       $password = ConvertTo-SecureString "myP@ssw0rd" –asplaintext –force

       if(-not (Get-Module AzureRm.Profile)) {
         Import-Module AzureRm.Profile
       }

       $resourceAppIdURI = "https://analysis.windows.net/powerbi/api"
       $authority = "https://login.microsoftonline.com/common/oauth2/authorize";
       $credentials = New-Object System.Management.Automation.PSCredential $Username,$password
       $AADcredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" -ArgumentList $credentials.UserName,$credentials.Password
       $authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority

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

       return $authResult
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM