使用Powershell更改SSRS报告的数据源

时间:2012-02-07 15:11:44

标签: powershell datasource reportingservices-2005

我正在尝试使用Powershell将多个SSRS报告的数据源更改为我的报告服务器上的一个共享数据源。这是我的代码:

cls;  
$reportserver = "myServer";<br/>
$url = "http://$($reportserver)/reportserver/reportservice2005.asmx?WSDL";";<br/>
$ssrs = New-WebServiceProxy -uri $url -UseDefaultCredential -Namespace "ReportingWebService";

[ReportingWebService.DataSource[]] $myDataSource = new-object ReportingWebService.DataSource
$myDataSource[0].Name = "myDS"";<br/>
$myDataSource[0].Item = New-Object ReportingWebService.DataSourceReference<br/>
$myDataSource[0].Item.Reference = "/Data Sources/MyDS"<br/>

$reports = $ssrs.ListChildren('/DH', $false)

$reports | ForEach-Object {<br/>
$reportPath = $_.path<br/>
 Write-Host "Report: " $reportPath<br/>
 $dataSources = $ssrs.GetItemDataSources($reportPath)<br/>
 $dataSources | ForEach-Object {<br/>
              Write-Host "Old source: $($_.Name), $($_.Item.ConnectString)"<br/>
              $ssrs.SetItemDataSources($reportPath, $myDataSource)<br/>
              Write-Host "New source: $($_.Name), $($_.Item.ConnectString)"<br/>
          }<br/>

 Write-Host "------------------------"
}

但是在调用“SetItemDataSources”时遇到以下错误 - 方法:

***Argument "1" having the value "ReportingWebService.DataSource[]" of "SetItemDataSources" can not be converted to type "ReportingWebService.DataSource[]".***

问题是:出了什么问题?类型是相同的!

3 个答案:

答案 0 :(得分:13)

谢谢e82.eric!

你带我到了工作的解决方案。这是:

cls;

#Set variables:
$reportserver = "myServer";
$newDataSourcePath = "/Data Sources/MyDS"
$newDataSourceName = "MyDS";
$reportFolderPath = "/DH"
#------------------------------------------------------------------------

$ssrs = New-WebServiceProxy -uri $url -UseDefaultCredential

$reports = $ssrs.ListChildren($reportFolderPath, $false)

$reports | ForEach-Object {
            $reportPath = $_.path
            Write-Host "Report: " $reportPath
            $dataSources = $ssrs.GetItemDataSources($reportPath)
            $dataSources | ForEach-Object {
                            $proxyNamespace = $_.GetType().Namespace
                            $myDataSource = New-Object ("$proxyNamespace.DataSource")
                            $myDataSource.Name = $newDataSourceName
                            $myDataSource.Item = New-Object ("$proxyNamespace.DataSourceReference")
                            $myDataSource.Item.Reference = $newDataSourcePath

                            $_.item = $myDataSource.Item

                            $ssrs.SetItemDataSources($reportPath, $_)

                            Write-Host "Report's DataSource Reference ($($_.Name)): $($_.Item.Reference)"
                            }

            Write-Host "------------------------" 
            }

答案 1 :(得分:10)

我遇到了同样的问题。

根据http://www.vistax64.com/powershell/273120-bug-when-using-namespace-parameter-new-webserviceproxy.html,它不是一个很好的解决方案。 New-WebServiceProxy的namespace参数有点破坏。帖子建议使用自动生成的命名空间,最终为我工作,所以我认为你可以做到这一点。

$reportserver = "myServer"
$url = "http://$($reportserver)/reportserver/reportservice2005.asmx?WSDL"
$ssrs = New-WebServiceProxy -uri $url -UseDefaultCredential

$proxyNamespace = $ssrs.GetType().Namespace

$myDataSource = New-Object ("$proxyNamespace.DataSource") 
$myDataSource[0].Name = "myDS"
$myDataSource[0].Item = New-Object ("$proxyNamespace.DataSourceReference")
$myDataSource[0].Item.Reference = "/Data Sources/MyDS"

$reports = $ssrs.ListChildren('/DH', $false)

$reports | ForEach-Object {
$reportPath = $.path
Write-Host "Report: " $reportPath
$dataSources = $ssrs.GetItemDataSources($reportPath)
$dataSources | ForEach-Object {
Write-Host "Old source: $($.Name), $($.Item.ConnectString)"
$ssrs.SetItemDataSources($reportPath, @($myDataSource))
Write-Host "New source: $($.Name), $($_.Item.ConnectString)"
}

Write-Host "------------------------" }

答案 2 :(得分:4)