使用Powershell刷新Power BI数据集

时间:2018-10-08 07:30:35

标签: powershell powerbi

我一直在使用此powershell脚本刷新Power BI中适用于我的数据集,但是问题是,当我运行该脚本时,总是有一个弹出窗口供我登录到Power BI帐户。我真的很想知道是否有办法让脚本自动登录?谢谢

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

1 个答案:

答案 0 :(得分:0)

通过以下内容,您可以轻松访问组信息:

$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

此处的clientId是使用https://dev.powerbi.com/apps创建的“本地” AAD客户端的ID。

要发送刷新命令,请使用Urls:

如果数据集在您自己的工作空间中:

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

否则,当它在组工作区中时:

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

现在将作业发送到powerBI服务器:

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

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

另请参阅博客文章:https://blog.gbrueckl.at/2017/08/refresh-powerbi-datasets-powershell-azure-runbooks/