用Power shell脚本批量发布rdl文件到SQL Server Reporting Service
本文用于介绍如何用Power shell脚本批量发布rdl文件到SQL Server Reporting Service.
用户可根据自己的需要创建类似Publish_All_SSRS.ps1的脚本。
目录
1. 目录结构
2. 创建Base_PublishSSRS.ps1
3. 创建Publish_All_SSRS.ps1
4.注意事项
1. 目录结构
目录结构:
根目录:C:\Reports
子目录:子目录里面全是rdl文件, 如NoticeMainReport.rdl
C:\Reports\MainNoticeReports
C:\Reports\JobTickets
C:\Reports\SSRSReports
2. 创建Base_PublishSSRS.ps1
# 以下的param参数会被Push_All_SSRS.ps1调用的时候替换掉,此处放置默认参数只是为了方便知道参数
param([Parameter(Mandatory=$false)][string]$ReportServerUrL = "http://10.236.155.32:20070/ReportServer/ReportService2010.asmx?wsdl", [Parameter(Mandatory=$false)][string]$LocalRDLFilePath = "C:\Reports\MainNoticeReports",[Parameter(Mandatory=$false)][string]$ReportFolderPath = "/MainNoticeReports",[Parameter(Mandatory=$false)][string]$SharedDataSourcePath = "/Agency",[Parameter(Mandatory=$false)][validateSet("Initialize","UploadRDL","UpdateDS")][string]$Action = "Initialize"
)
Function Get-ReportServerInstance{param([Parameter(Mandatory=$true)][string]$ReportUrl,[Parameter(Mandatory=$false)][string]$Namespace)return New-WebServiceProxy -Uri $ReportUrl -UseDefaultCredential -Namespace $Namespace}Function Delete-ItemsByPath{param([Parameter(Mandatory=$true)][object]$RS,[Parameter(Mandatory=$true)][string]$Path,[Parameter(Mandatory=$false)][bool]$IsRecurse=$false)$RS.DeleteItem($Path)if($IsRecurse){$RS.ListChildren($Path,$IsRecurse) | %{$RS.DeleteItem($_.Path)}}}Function Upload-ReportToRemoteServer{param([Parameter(Mandatory=$true)][object]$RS,[Parameter(Mandatory=$true)][string]$RDLFilePath,[Parameter(Mandatory=$true)][string]$ReportServerPath)Resolve-Path -Path $RDLFilePath$RDLFile = Get-Item -Path $RDLFilePath$reportName = [System.IO.Path]::GetFileNameWithoutExtension($RDLFile.Name)$bytes = [System.IO.File]::ReadAllBytes($RDLFile.FullName)$warnings=$null$report = $rs.CreateCatalogItem("Report", # Catalog item type$reportName, # Report name$ReportServerPath,# Destination folder$true, # Overwrite report if it exists?$bytes, # .rdl file contents$null, # Properties to set.[ref]$warnings) # Warnings that occured while uploading.$warnings | %{ Write-Output ("Warning: {0}" -f $_.Message)}}Function Create-ReportFolder{Param([Parameter(Mandatory=$true)][object]$RS,[Parameter(Mandatory=$true)][string]$FolderName,[Parameter(Mandatory=$false)][string]$ParentFolderPath="/")$RS.CreateFolder($FolderName,$ParentFolderPath,$null)}Function Test-ItemByPath{Param([Parameter(Mandatory=$true)][object]$RS,[Parameter(Mandatory=$true)][string]$Path)Return ($Rs.ListChildren("/",$true) | ?{$_.Path -eq $Path}) -ne $null}Function Update-DataSource{Param([Parameter(Mandatory=$true)][object]$RS,[Parameter(Mandatory=$true)][string]$SharedDataSourcePath,[Parameter(Mandatory=$true)][string]$ReportFolderPath)$RS.ListChildren($ReportFolderPath,$false) |?{$_.TypeName -eq "Report"} | %{$referencedDataSourceName = (@($rs.GetItemReferences($_.Path, "DataSource")))[0].Name# Change the datasource for the report to $SharedDataSourcePath# Note that we can access the types such as DataSource with the prefix # "SSRS" only because we specified that as our namespace when we # created the proxy with New-WebServiceProxy.$dataSource = New-Object SSRS.DataSource$dataSource.Name = $referencedDataSourceName # Name as used when designing the Report$dataSource.Item = New-Object SSRS.DataSourceReference$dataSource.Item.Reference = $SharedDataSourcePath # Path to the shared data source as it is deployed here.$rs.SetItemDataSources($_.Path, [SSRS.DataSource[]]$dataSource) }}$Rs = Get-ReportServerInstance -ReportUrl $ReportServerUrL -Namespace "SSRS"
if($Action -eq "Initialize")
{if((Test-ItemByPath -RS $RS -Path $ReportFolderPath) -eq $false){Create-ReportFolder -RS $RS -FolderName $ReportFolderPath.remove(0,1)}Resolve-Path $LocalRDLFilePathGet-ChildItem -Path $LocalRDLFilePath | ?{$_.Name -like "*.rdl"} | %{Upload-ReportToRemoteServer -RS $RS -RDLFilePath $_.FullName -ReportServerPath $ReportFolderPath }$RS = $null
}
elseif($Action -eq "UpdateDS")
{Update-DataSource -RS $RS -SharedDataSourcePath $SharedDataSourcePath -ReportFolderPath $ReportFolderPath$RS=$null
}
3. 创建Publish_All_SSRS.ps1
param( [Parameter(Mandatory=$false)][string]$ReportServerUrL = "http://10.236.155.32:20070/ReportServer/ReportService2010.asmx?wsdl",[Parameter(Mandatory=$false)][string]$ParentRDLFilePath = "C:\Reports\",# 你的rdl所在的路径[Parameter(Mandatory=$false)][string]$SSRSPathPrefix = "ChinaDev_",[Parameter(Mandatory=$false)][validateSet("Initialize","UploadRDL","UpdateDS")][string]$Action = "Initialize"
)$LocalRDLFilePath1 = $ParentRDLFilePath + "MainReports" # Reports目录下面的子目录,里面就是rdl文件了
$LocalRDLFilePath2 = $ParentRDLFilePath + "JobTickets" # Reports目录下面的子目录,里面就是rdl文件了
$LocalRDLFilePath3 = $ParentRDLFilePath + "SSRSReports" # Reports目录下面的子目录,里面就是rdl文件了
$ReportFolderPath1 = "/" + $SSRSPathPrefix + "MainReports" #SSRS 上面的目录结构
$ReportFolderPath2 = "/" + $SSRSPathPrefix + "JobTickets" #SSRS 上面的目录结构
$ReportFolderPath3 = "/" + $SSRSPathPrefix + "SSRSReports" #SSRS 上面的目录结构
$remoteReportDataSource1 = $ReportFolderPath1 + '/Agency' #SSRS 上面的默认数据库连接
$remoteReportDataSource2 = $ReportFolderPath2 + '/Agency' #SSRS 上面的默认数据库连接
$remoteReportDataSource3 = $ReportFolderPath3 + '/Agency' #SSRS 上面的默认数据库连接$BasePS1 = $PSScriptRoot+"\Base_PublishSSRS.ps1"
Write-host "::::::::::::::::::::::::::::::::Update AgencyNotice, JobTicket, SSRS: "$Action
Write-host "::::::::::::::::::::::::::::::::MainReportsStart:"
& $BasePS1 -ReportServerUrL $ReportServerUrL -LocalRDLFilePath $LocalRDLFilePath1 -ReportFolderPath $ReportFolderPath1 -SharedDataSourcePath $remoteReportDataSource1 -Action $ActionWrite-host "::::::::::::::::::::::::::::::::JobTickets Start:"
& $BasePS1 -ReportServerUrL $ReportServerUrL -LocalRDLFilePath $LocalRDLFilePath2 -ReportFolderPath $ReportFolderPath2 -SharedDataSourcePath $remoteReportDataSource2 -Action $ActionWrite-host "::::::::::::::::::::::::::::::::SSRSReports Start:"
& $BasePS1 -ReportServerUrL $ReportServerUrL -LocalRDLFilePath $LocalRDLFilePath3 -ReportFolderPath $ReportFolderPath3 -SharedDataSourcePath $remoteReportDataSource3 -Action $Action
4.注意事项
运行前需要确定你的域账号有访问SSRS的读写权限。
如果你当前的域账号没有权限,可以把Reports目录拷贝到SSRS服务器上再执行。