簡體   English   中英

使用 Powershell 批量更改 SSRS 數據源

[英]Bulk Change SSRS Data Sources using Powershell

我正在嘗試更改一組 Reporting Services 報告的數據源,但我無法讓 Powershell 為它們工作。 我會很感激任何幫助:)

$server = "http://My/ReportServer/"
$dataSource = Get-RsDataSource -Path "/Data Sources/NewDataSource" - 
ReportServerUri $server

$reports = Get-RsCatalogItems -RsFolder "/Testing/NewDataSOurce" -ReportServerUri $server -Recurse | Where-Object {$_.TypeName -eq "Report"}

$reports | ForEach-Object {
    $reportDataSource = Get-RsItemDataSource -RsItem $_.Path -ReportServerUri $server
    $reportPath = $_.Path
    if ($reportDataSource.Name -eq "OldDataSource") {
        Set-RsItemDataSource -RsItem $reportPath -DataSource $dataSource -ReportServerUri $server
    }
}

我寫了一個函數來做你正在談論的設置數據源。 這是我的...不幸的是我不再有SSRS實例了。 完整的腳本/模塊是我的GitHub帳戶的要點。 我會在這個帖子的底部粘貼我的主要網址。

我將代碼片段拉出的功能稱為Deploy-NativeSSRS。 我使用這個模塊+一個驅動程序腳本來推送已經檢出TFS的項目。 因此,他們可以在CI活動期間進行解析,然后推送到SSRS。

$reports = New-ReportObject -files (Get-ChildItem -Path $reportPath -Filter $reportExtension) 
foreach($report in (($reports | Where-Object{$_.datasourcename -eq $datasourceName}).filename))

    {

        $fileExt = $reportExtension.trim('*')

        $status = Set-SSRSDataSourceInfoNative -ReportName ($report.trim($fileext)) -reportPath $documentLibrary -DataSourceName $datasourceName -DataSourcePath "$dataSourceTarget/$datasourceName"  -reportWebService $webservice

        write-output "The following $report datasource was updated to $datasourcename"

    }  

function set-SSRSDataSourceInfoNative
{
    param
    (
        [parameter(mandatory)]
        [string]$Reportname, #with no extension SSRS has no name for the file in native mode
        [parameter(mandatory)]
        [string]$reportPath,
        [parameter(mandatory)]
        [string]$DataSourceName,
        [parameter(mandatory)]
        [string]$DataSourcePath,
        [parameter(mandatory)]
        [uri]$reportWebService,
        [System.Management.Automation.PSCredential]$Credentials 
    )
    if ($Credentials)
    {$reportProxy = new-webserviceproxy -uri $reportWebService -Credential $credentials  -namespace 'SSRSProxy' -class 'ReportService2010'}
    else
    {$reportProxy = new-webserviceproxy -uri $reportWebService -UseDefaultCredential  -namespace 'SSRSProxy' -class 'ReportService2010'}
    $f = $ReportName.ToLower()
    try
    {
        $dataSources = $reportProxy.GetItemDataSources("$reportpath/$reportname")
    }
    catch
    {
        "Error was $_"
        $line = $_.InvocationInfo.ScriptLineNumber
        "Error was in Line $line"
        "ReportName: $reportname"
        "ReportPath: $reportpath"
    }
    $proxyNameSpace = $dataSources.gettype().Namespace
    $dc = $reportProxy.GetDataSourceContents($DataSourcePath)
    if ($dc)
    { 
        $d = $dataSources | Where-Object {$_.name -like $DataSourceName }
        $newDataSource = New-Object ("$proxyNameSpace.DataSource")
        $newDataSource.Name = $datasourcename
        $newDataSource.Item = New-Object ("$proxyNamespace.DataSourceReference")
        $newDataSource.Item.Reference = $DatasourcePath 
        $d.item = $newDataSource.item
        $reportProxy.SetItemDataSources("$reportpath/$f", $d)
        $set = ($reportproxy.GetItemDataSources("$reportPath/$f")).name
        write-verbose "$reportname set to data source $set"
        $returnobj = 'success'
    }
    $returnobj
}

https://gist.github.com/crshnbrn66/40c6be436e7c2e69b4de5cd625ce0902 https://gist.github.com/crshnbrn66/b10e43ef0dadf7f4eeae620428b2cdd9

這是與 Power BI 報表服務器 Rest API 一起使用的東西:

[string] $uri = "https://xxx/Reports"
$session = New-RsRestSession -ReportPortalUri $uri
$reports = Get-RsRestFolderContent -WebSession $session -RsFolder / -Recurse | Where-Object {$_.Type -eq "PowerBIReport"}
 
 
$reports | ForEach-Object {
$dataSources = Get-RsRestItemDataSource -WebSession $session -RsItem $_.Path | Where-Object {$_.ConnectionString -eq "yyy;zzz"}
#$dataSources[0].DataModelDataSource.AuthType = 'Windows'
$dataSources[0].DataModelDataSource.Username = 'domain\user'
$dataSources[0].DataModelDataSource.Secret = 'password'
Set-RsRestItemDataSource -WebSession $session -RsItem $_.Path -RsItemType 'PowerBIReport' -DataSources $dataSources
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM