使用 PowerShell 變更 Power BI 報表中的數據源 連接字串 - Power BI 報表伺服器
我們可讓您更新Power BI報表的 DirectQuery 和重新整理連線。
重要
這也是您在先前版本中如何設定這項功能的重大變更。 如果您使用 2020 年 10 月前版本的 Power BI 報表伺服器,請參閱使用 PowerShell 變更 Power BI 報表中的數據源 連接字串 - Power BI 報表伺服器 2020 年 10 月前
必要條件
- 下載最新版的 Power BI 報表伺服器 和 Power BI Desktop for Power BI 報表伺服器。
- 已使用針對報表伺服器優化的Power BI Desktop 2020年10月或更新版本儲存的報表,並 已啟用增強式數據集元數據 。
- 使用參數化連接的報告。 發佈之後,只能更新具有參數化連接和資料庫的報表。
- 此範例使用 Reporting Services PowerShell 工具。 您可以使用新的 REST API 來達成相同的目標。
建立具有參數化連線的報表
建立與伺服器的 SQL Server 連線。 在下列範例中,我們會連線到 localhost 至名為 ReportServer 的資料庫,並從 ExecutionLog 提取數據。
以下是 M 查詢目前的樣子:
let Source = Sql.Database("localhost", "ReportServer"), dbo_ExecutionLog3 = Source{[Schema="dbo",Item="ExecutionLog3"]}[Data] in dbo_ExecutionLog3
在 Power Query 編輯器 功能區中選取 [管理參數]。
建立 servername 和 databasename 的參數。
編輯第一個連線的查詢,並對應資料庫和伺服器名稱。
現在查詢看起來像這樣:
let Source = Sql.Database(ServerName, Databasename), dbo_ExecutionLog3 = Source{[Schema="dbo",Item="ExecutionLog3"]}[Data] in dbo_ExecutionLog3
- 將該報表發佈至伺服器。 在此範例中,報表名為 executionlogparameter。 下圖是數據源管理頁面的範例。
使用 PowerShell 工具更新參數
開啟 PowerShell 並安裝最新的 Reporting Services 工具,請遵循 的 https://github.com/microsoft/ReportingServicesTools指示。
若要取得報表的參數,請使用新的 REST DataModelParameters API,並使用下列 PowerShell 呼叫:
Get-RsRestItemDataModelParameters '/executionlogparameter' Name Value ---- ----- ServerName localhost Databasename ReportServer
我們會將此呼叫的結果儲存在變數中:
$parameters = Get-RsRestItemDataModelParameters '/executionlogparameter'
對應至字典以存取參數值。
$parameterdictionary = @{} foreach ($parameter in $parameters) { $parameterdictionary.Add($parameter.Name, $parameter); }
此變數會以我們需要變更的值進行更新。
更新所需參數的值:
$parameterdictionary[“ServerName”].Value = 'myproductionserver' $parameterdictionary[“Databasename”].Value = 'myproductiondatabase'
透過更新的值,我們可以使用 commandlet
Set-RsRestItemDataModelParameters
來更新伺服器中的值:Set-RsRestItemDataModelParameters -RsItem '/executionlogparameter' -DataModelParameters $parameters $parameterdictionary.Values
更新參數之後,伺服器會更新係結至參數的任何數據源。 回到 [ 編輯資料源] 對話框,您應該能夠設定更新伺服器和資料庫的認證。
相關內容
更多問題嗎? 嘗試在 Power BI 社群提問
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應