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

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

http://www.xdnf.cn/news/3324.html

相关文章:

  • 详细介绍C++中指针解引用
  • 枚举法——C++算法【泪光2929】
  • ShardingSphere5详细笔记
  • Vue2 和 Vue3 的核心区别
  • 腾讯云web服务器配置步骤是什么?web服务器有什么用途?
  • TM1668芯片学习心得二
  • 【SpringBoot】基于mybatisPlus的博客系统
  • 【计算机视觉】目标检测:深度解析MMDetection:OpenMMLab开源目标检测框架实战指南
  • Winform(6.序列化方式)
  • 港口危货储存单位主要安全管理人员考试精选题目
  • [Unity]设置自动打包脚本
  • Copilot 祝你走在AI前沿:2025 年 4 月动态
  • 小程序中的页面跳转
  • TimeDistill:通过跨架构蒸馏的MLP高效长期时间序列预测
  • 有状态服务与无状态服务:差异、特点及应用场景全解
  • leetcode76
  • Vue+tdesign t-input-number 设置长度和显示X号
  • 智能驾驶新时代:NVIDIA高级辅助驾驶引领未来出行安全
  • iOS 性能调优实战:三款工具横向对比实测(含 Instruments、KeyMob、Xlog)
  • C语言与Unix的传奇起源
  • (32)VTK C++开发示例 ---背景纹理
  • pytorch中的变量内存分配
  • WPF之RadioButton控件详解
  • C/C++核心机制深度解析:指针、结构体与动态内存管理(面试精要)
  • 生成项目.gitignore文件的多种高效方式
  • 分布式-redisson
  • 优先级队列
  • 【DBeaver】如何连接MongoDB
  • VSCode Auto Rename Tag插件不生效
  • OLED技术解析与驱动实战指南