代码之家  ›  专栏  ›  技术社区  ›  Pedro Martins de Souza Regeesh Chandran

Azure Powershell自动登录以刷新电源BI

  •  0
  • Pedro Martins de Souza Regeesh Chandran  · 技术社区  · 7 年前

    我目前正在开发一个脚本,使用PowerBI的RESTAPI自动刷新我的一个PowerBI数据集。这个脚本是一个.ps1文件,从我的Python代码中调用,如下所示。我一步一步地跟着他 this 教程,而我的 刷新.ps1 文件是从 this official MS source. 为了让它运行,只做了一些小的修改,所以如果有人想测试它,我建议使用我在问题末尾发布的代码。

    但是,除此之外,这两个代码都工作得很好,我认为它们允许我刷新数据集, 每次它们运行时,我都必须通过提示的GUI手动登录我的Azure帐户 . 这使得该脚本无法自动执行任务,例如在我的例子中。请参见下面的GUI图片:

    enter image description here

    我做了一些参考资料,但找不到任何类似的案例,其中也使用了Python。关于Powershell,我阅读了关于使用ADAL的内容,但无法真正了解ADAL是什么。

    到目前为止,我的Python脚本是:

    import subprocess, sys
    
    def powershell(file):
    
        p = subprocess.Popen(["powershell.exe", file], stdout=sys.stdout)
        p.communicate()
    
    MS.powershell(r"'C:\blablabla\refresh.ps1'")
    

    refresh.ps1 文件的内容如下所示:

    $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
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Andrey Nikolov user3098458    7 年前

    如果要以静默方式进行身份验证,可以使用 UserCredentials 类,并将用户名和密码传递给AcquireToken方法。GetAuthToken函数应该如下所示:

    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
    }