首页 > 解决方案 > 使用 powershell 在线登录到 power bi 服务(静默,即没有弹出窗口)

问题描述

我想登录到 Power BI Online 服务并使用 REST API 从数据集中删除行。我的其余代码运行良好,但登录似乎不起作用。这是我尝试过的。有人能帮助我吗?谢谢!

$pbiUsername = "abc.xyz@xxx.com"
$pbiPassword = "Password"
$clientId = "a81b2cc1-4c97-2323-bal4-eeb21c4c6e46"

$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

标签: powershellpowerbi

解决方案


“登录似乎不起作用”并没有为我们提供足够的信息来提示您出了什么问题。

我会推荐你​​使用官方的Microsoft Power BI Cmdlet来完成这样的任务。它有很大的优势 - 您无需注册应用程序即可使用它。在这种情况下,您的代码如下所示:

Import-Module MicrosoftPowerBIMgmt
Import-Module MicrosoftPowerBIMgmt.Profile

$password = "Password" | ConvertTo-SecureString -asPlainText -Force
$username = "abc.xyz@xxx.com" 
$credential = New-Object System.Management.Automation.PSCredential($username, $password)

Connect-PowerBIServiceAccount -Credential $credential

Invoke-PowerBIRestMethod -Url 'groups/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/datasets/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/tables/xxxxxx/rows' -Method Delete

Disconnect-PowerBIServiceAccount

推荐阅读