使用Powershell在线登录Power Bi服务(静默即没有弹出窗口)

时间:2019-02-10 21:33:04

标签: powershell powerbi

我想登录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

2 个答案:

答案 0 :(得分:0)

“登录似乎无法正常工作”并没有提供足够的信息来提示您问题所在。

我建议您使用官方的Microsoft Power BI Cmdlets来完成类似的任务。它具有很大的优势-您无需注册应用程序即可使用它。在这种情况下,您的代码如下所示:

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

答案 1 :(得分:0)

由于PowerBI现在已启用 Service Principal ,因此您无需担心用户名和密码,更重要的是,您不需要PRO许可证

使用服务主体,您现在可以登录而无需弹出窗口以及用户名,密码

$applicationId = "xxxxxxxx";
$securePassword = "xxxxxx" | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $applicationId, $securePassword
Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credential -TenantId "xxxx"