[英]Can you use the results of a Power BI Report Server report as a data source in a C#, Python or PowerShell program?
[英]Set report data source and credentials on Power BI Report Server through REST API to establish a scheduled refresh
我的任務如下:使用 Power BI Report Server (PBIRS) REST API ,上傳嵌入式報告(嵌入式意味着數據模型集成在報告本身中 - 從 SQL Server 數據庫簡單導入,因此沒有實時連接或DirectQuery 等)到 PBIRS,設置憑據(ConnectionString、Windows 身份驗證用戶名和密碼)以訪問數據源(數據庫)並設置每日計划刷新。
使用 Powershell 腳本,我可以毫無問題地上傳報告,但是當我嘗試將憑據設置為數據源時會出現問題。 這是我目前必須完成的代碼:
$payload0 =
'
{
"Name": "Data Source test",
"Description": "string",
"Path": "/Valid/Path",
"Type": "DataSource",
"Hidden": false,
"Size": 0,
"ModifiedBy": "string",
"ModifiedDate": "2020-01-13T15:51:04Z",
"CreatedBy": "string",
"CreatedDate": "2020-01-13T15:51:04Z",
"IsFavorite": false,
"IsEnabled": true,
"ConnectionString": "valid.connection\\string",
"DataModelDataSource": {
"AuthType": "Windows",
"SupportedAuthTypes": [
"Windows"
],
"Kind": "SQL",
"ModelConnectionName": "string",
"Secret": "",
"Type": "Import ",
"Username": "myUserName"
},
"DataSourceSubType": "DataModel",
"DataSourceType": "SQL",
"IsOriginalConnectionStringExpressionBased": false,
"IsConnectionStringOverridden": false,
"CredentialRetrieval": "prompt",
"CredentialsByUser": {
"DisplayText": "someText",
"UseAsWindowsCredentials": true
},
"CredentialsInServer": {
"UserName": "myUserName",
"Password": "myPassword",
"UseAsWindowsCredentials": true,
"ImpersonateAuthenticatedUser": true
},
"IsReference": false
}
'
$restApiUri = $ReportPortalUri + "/api/v2.0/DataSources"
Invoke-RestMethod -Uri $restApiUri -Method Post -Body $payload0 -ContentType "application/json" -UseDefaultCredentials -UseBasicParsing -Verbose
運行代碼后,我得到一個201 Created
響應 JSON,指示數據源及其訪問憑據的成功創建。 如果我檢查 PBIRS 報告的“ Manage
窗格,我看不到對數據源所做的任何更改。
我的下一步是通過在報表服務器上輸入我的用戶名和密碼來手動設置憑據,然后執行以下代碼來設置計划刷新:
$payload1 =
'
{
"ParameterValues":[
],
"Description":"testSchedule",
"CatalogItemPath":"/Valid/Path",
"EventType":"DataModelRefresh",
"Schedule":{
"Definition":{
"EndDate":"0001-01-01T00:00:00Z",
"StartDateTime":"2019-01-15T02:00:00Z",
"EndDateSpecified":false,
"Recurrence":{
"DailyRecurrence": {
"DaysInterval":1
}
}
}
}
}
'
$restApiUri = $ReportPortalUri + "/api/v2.0/CacheRefreshPlans"
Invoke-RestMethod -Uri $restApiUri -Method Post -Body $payload1 -ContentType "application/json" -UseDefaultCredentials -UseBasicParsing -Verbose
鑒於我事先手動設置了數據源的憑據,運行此代碼會成功創建計划刷新。
所以我的問題簡而言之:如何使用 PBIRS REST API 設置數據源(由有效 ConnectionString 定義的 SQL Server 連接)的憑據(Windows 身份驗證和用戶名/密碼)?
任何幫助,將不勝感激!
事實證明,還有另一種方法可以完成任務,即完全使用不同的 API。 使用ReportingServicesTools方法,我們可以將報告上傳到 PBIRS並通過以下方式設置其數據源憑據:
# create PBIRS session
Write-Output "Creating a session to the Report Server $ReportPortalUri"
$session = New-RsRestSession -ReportPortalUri $ReportPortalUri
# upload report with overwrite
write-output "Upload report with overwrite"
Write-RsRestCatalogItem -WebSession $session -Path $FilePath -RsFolder $RsFolder -Description $Description -Overwrite
# get report name
$reportName = [System.IO.Path]::GetFileNameWithoutExtension($FilePath)
# get existing data source properties
$dataSource = Get-RsRestItemDataSource -WebSession $session -RsItem "$RsFolder/$reportName"
# setting up data source credentials
$dataSource.DataModelDataSource.AuthType = "Windows"
$dataSource.CredentialRetrieval = "Store"
$dataSource.DataModelDataSource.UserName = "myUsername"
$dataSource.DataModelDataSource.Secret = "myPassword"
# applying credentials
Set-RsRestItemDataSource -WebSession $session -RsItem "$RsFolder/$reportName" -RsItemType "PowerBIReport" -DataSources $dataSource
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.