如何获取所有报表的数据源用户名?

How to get data source usernames of ALL reports?

基于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

期望的输出:像这样的列表

如果有多个数据源connection/report,分别在每一行列出用户名

如何实现?

我正在尝试将脚本调整为类似这样的内容,但不确定遍历项目的正确方法:

# 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
    })
}