当前位置: 首页 > news >正文

用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服务器上再执行。

相关文章:

  • Android Framework框架与启动过程初识一
  • uniapp如何获取安卓原生的Intent对象
  • 分布式-redisson
  • 关于 live555延迟优化之缓存区优化“StreamParser::afterGettingBytes() warning: read”” 的解决方法
  • PLC与工业电脑:有什么区别?
  • 精益数据分析(35/26):SaaS商业模式关键指标解析
  • AI生成Flutter UI代码实践(一)
  • 【MongoDB篇】MongoDB的文档操作!
  • 运维打铁: 存储方案全解析
  • 【MongoDB篇】MongoDB的索引操作!
  • 文章记单词 | 第52篇(六级)
  • day11 python超参数调整
  • 32单片机——串口
  • C++初阶-string类1
  • Hadoop虚拟机中配置hosts
  • 微信小程序 XSS 防护知识整理
  • 上海车展,世界模型 + VLA,城区智驾进入下半场
  • 同时启动俩个tomcat压缩版
  • spring--事务详解
  • react-10样式模块化(./index.module.css, <div className={welcome.title}>Welcome</div>)
  • 网警查处编造传播“登顶泰山最高可得3万奖金”网络谣言者
  • “80后”蒋美华任辽宁阜新市副市长
  • 交通运输部:预计今年五一假期全社会跨区域人员流动量将再创新高
  • 我国首部《人工智能气象应用服务办法》今天发布
  • 从腰缠万贯到债台高筑、官司缠身:尼泊尔保皇新星即将陨落?
  • 中共中央、国务院关于表彰全国劳动模范和先进工作者的决定