![](/img/trans.png)
[英]Powershell - automated connection to Power BI service without hardcoding password
[英]Login to power bi service online (silently i.e. without the popup) using powershell
我想登錄 Power BI Online 服務並使用 REST API 從數據集中刪除行。 我的代碼 rest 運行良好,但登錄似乎不起作用。 這是我嘗試過的。 有人能幫助我嗎? 謝謝!
$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
“登錄似乎不起作用”沒有給我們足夠的信息來提示您問題所在。
我建議您使用官方的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
由於PowerBI現在已啟用服務主體 ,因此您無需擔心用戶名和密碼,更重要的是,您無需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"
該錯誤未通過 try catch 處理 - 您仍然會遇到相同的錯誤: try {Connect-PowerBIServiceAccount} catch {"Boo"}
我按照步驟來解決這個問題。
解決方案要解決此問題,我們需要安裝正確的模塊。 它可能不是最新的構建,但構建必須匹配才能為我們工作。 在我的場景中,我安裝了 MicrosoftPowerBIMgmt.Workspaces 1.0.830,使其與 MicrosoftPowerBIMgmt.Profile 匹配。
我去了 PowerShell Gallery 獲取安裝模塊 cmd。 https://www.powershellgallery.com/packages/MicrosoftPowerBIMgmt/1.0.326
注意:請記住,對於我的實驗室場景,我使用了 MicrosoftPowerBIMgmt.Workspaces 模塊。 您將需要安裝您關注的模塊。
注意:您必須啟動一個新的 PowerShell session。 如果您不這樣做,錯誤可能不會 go 消失。
https://dastrongman.wordpress.com/2020/01/11/pbiwiki-login-with-the-power-bi-service-account/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.