How to set correct DataSource in PowerShell for SSRS reports

2.2k Views Asked by At

I have a PowerShell script that should set the correct data source for a report.
I have more data sources for example DS1 and DS2.

Here is part of my PowerShell code:

function UpgradeReport
{
    param
    (
        [string]$t,
        [string]$ReportFolder,
        [string]$ReportName,
        [string]$DataSourceFolder,
        [string]$IssueId
    )

if([string]::IsNullOrEmpty($DataSourceFolder)) { $DataSourceFolder="DS1" }

.......
# if all ok since now, report is uploaded
        # set datasource if new report
        if (!$reports.$reportName) {
            Write-Host $reportName ' is new. Setting data source: ' $DataSourceFolder
            $Proxy.SetItemDataSources("/$reportFolder/$reportName", $datasources.$DataSourceFolder)
        }
        Write-Output "$(Timestamp) Finished InstallReports for $ReportName"

.....

# get list of all datasources
$Proxy.ListChildren('/Data Sources', $false) | ForEach-Object { $datasources.add($_.Name,$_ ) }

The problem is probably in SetItemDataSources where I have $datasources.$DataSourceFolder

If someone knows how I can fix this I will be happy.

1

There are 1 best solutions below

0
On BEST ANSWER

I am using primarily SSRS 2008 but I am sure your issue is that the overload for SetItemDataSources is looking for a DataSource object. Not just a path to the object.

$newDataSource = New-Object "$ssrsServiceNamespace.DataSource"
$newDataSource.Name = $DataSourceName
$newDataSource.Item = New-Object ("$ssrsServiceNamespace.DataSourceReference")
$newDataSource.Item.Reference = $DataSourcePath
Write-Verbose "New Datasource Name     : $($newDataSource.Name)"
Write-Verbose "New Datasource Reference: $($newDataSource.Reference)"

$ReportService.SetItemDataSources($reportPath, $newDataSource)

Notes about the above code:

$ssrsServiceNamespace.DataSource comes from the connection object. It creates an instance specific DataSource object. For me, making a generic one ended up with type conversion errors causing SetItemDataSources() to fail. $ReportService.Gettype().Namespace. So for me that works out to Microsoft.PowerShell.Commands.NewWebserviceProxy.AutogeneratedTypes.WebServiceProxy1tServer_ReportService2005_asmx and my way look nicer and theoretically helps my code be more portable between SSRS environments.