如何解决如何获取所有报表的数据源用户名?
基于 this MSDoc here,我编写了以下脚本来获取单个 RsItem 的数据源,但我们有 800 多个报告,因此我想获得所有报告用户名的详细列表(我不想指定每个RsItem)
Install-Module ReportingServicesTools
# establish session w/ Report Server
$session = New-RsRestSession -ReportPortalUri https://sql-dev02.domain.com/Reports
# get data source object
$dataSources = Get-RsRestItemDataSource -WebSession $session -RsItem '/RefreshInfoSample'
# get username
$dataSources[0].DataModelDataSource.Username
期望输出:这样的列表
如果有多个数据源连接/报表,请分别在各行中列出用户名
如何实现这一目标?
我正在尝试将脚本调整为这样的内容,但不确定循环遍历项目的正确方法:
# establish session w/ Report Server
$session = New-RsRestSession -ReportPortalUri https://sql-dev02.domain.com/Reports
foreach (-RsItem $item) {
# get data source object
$dataSources = Get-RsRestItemDataSource -WebSession $session -RsItem $item
# get username
$dataSources[0].DataModelDataSource.Username
}
解决方法
我没有报告服务器来测试 (因此,要遵循伪代码)。您没有显示 $item
的来源或其中的内容,但在正常循环上下文中,这...
# establish session w/ Report Server
$session = New-RsRestSession -ReportPortalUri https://sql-dev02.domain.com/Reports
foreach (-RsItem $item) {
# get data source object
$dataSources = Get-RsRestItemDataSource -WebSession $session -RsItem $item
# get username
$dataSources[0].DataModelDataSource.Username
}
.. 不是有效的语法。如果您在 Powershell_ISE 或 VSCode 中执行此操作,IntelliSense 和 PScriptAnalyzer 将使此...
(-RsItem $item)
... 是错误代码。
上面应该是这样的...
$session = New-RsRestSession -ReportPortalUri 'https://sql-dev02.domain.com/Reports'
foreach ($item in $session )
{
$dataSources = Get-RsRestItemDataSource -WebSession $session -RsItem $item
$dataSources[0].DataModelDataSource.Username
}
...或类似的东西:
$session = New-RsRestSession -ReportPortalUri 'https://sql-dev02.domain.com/Reports' |
foreach {
$dataSources = Get-RsRestItemDataSource -WebSession $PSItem -RsItem $item
$dataSources[0].DataModelDataSource.Username
}
然而,这似乎是您正在尝试的列表:
$dataSources = Get-RsRestItemDataSource -WebSession $session -RsItem '/RefreshInfoSample'
所以,然后是这样的:
$session = New-RsRestSession -ReportPortalUri 'https://sql-dev02.domain.com/Reports'
Get-RsRestItemDataSource -WebSession $session -RsItem $item |
foreach {$PSItem[0].DataModelDataSource.Username}
如果您没有使用像 ISE/VSCode 这样的 PowerShell 编辑器,并且希望通过控制台主机查看您的代码,请使用 Invoke-ScriptAnalyzer
cmdlet...
# Get specifics for a module,cmdlet,or function
(Get-Command -Name Invoke-ScriptAnalyzer).Parameters
(Get-Command -Name Invoke-ScriptAnalyzer).Parameters.Keys
Get-help -Name Invoke-ScriptAnalyzer -Examples
Get-help -Name Invoke-ScriptAnalyzer -Full
Get-help -Name Invoke-ScriptAnalyzer -Online
...并查看代码上的调用堆栈以了解发生了什么,然后使用 Trace-Command
cmdlet。
(Get-Command -Name Trace-Command).Parameters
(Get-Command -Name Trace-Command).Parameters.Keys
Get-help -Name Trace-Command -Examples
Get-help -Name Trace-Command -Full
Get-help -Name Trace-Command -Online
,
感谢@postanote 的回答,以下是解决方案:
$webPortalURL = "https://sql-dev02.domain.com/Reports"
$PBI_Reports_Array = @()
$PBI_Reports_Array = $(Invoke-RestMethod -UseDefaultCredentials -uri $($webPortalURL + "/api/v2.0/PowerBIReports"))
$session = New-RsRestSession -ReportPortalUri $webPortalURL
foreach ($reportPath in $PBI_Reports_Array.value.path) {
Get-RsRestItemDataSource -WebSession $session -RsItem $reportPath | foreach {$PSItem[0].DataModelDataSource.Username}
}
另一种执行速度更快的方式:
# lists all the reports on the server that are available to your user account
Function listReports($baseURL) {
$reports = Invoke-RestMethod -UseDefaultCredentials -uri "$baseURL/api/v2.0/CatalogItems"
$reports.value | Where-Object {$_.Type -eq "PowerBIReport"} | foreach {
#Write-Host ("{0} {1}" -f $_.Id,$_.Name)
return $_.Id
}
}
Function getDataSources($baseURL,$reportID) {
$sources = Invoke-RestMethod -UseDefaultCredentials -uri "$baseURL/api/v2.0/PowerBIReports($reportID)/DataSources"
if ($sources.value -is [array]) {
return $sources.value
} else {
return @($sources.value)
}
}
$reportIDs = @(listReports("https://sql-dev02.domain.com/Reports"))
foreach($reportID in $reportIDs) {
(getDataSources "https://sql-dev02.domain.com/Reports" $reportID | % {
return $_.DataModelDataSource.Username
})
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。