You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用PowerShell自动快速获取SSRS指定报表并导出为CSV?

用PowerShell自动化SSRS报表导出CSV的实现方案

完全可以通过PowerShell实现这个自动化需求,以下是具体的实现步骤和脚本示例:

核心思路

利用SSRS的Web服务或REST API,传入指定域的参数,直接调用报表的导出功能生成CSV文件,还可以结合Windows任务计划实现每日自动执行。

方案一:使用SSRS Web服务代理(兼容性强)

适用于所有支持SSRS Web服务的版本,步骤如下:

  1. 配置参数:替换脚本中的占位值为你的实际信息
  2. 创建Web服务代理:连接到SSRS的服务端点
  3. 传入报表参数:指定目标域名
  4. 执行导出:将报表渲染为CSV并保存到本地
# 配置基础参数
$ssrsServer = "你的SSRS服务器地址"
$reportFullPath = "/ConfigMgr_BNL/Hardware/General/Computers in a specific workgroup or domain"
$targetDomain = "需要查询的域名"
$outputCsvPath = "C:\Reports\DailyComputers.csv"
$ssrsWsdlUrl = "http://$ssrsServer/ReportServer/ReportService2010.asmx?wsdl"

# 创建SSRS Web服务代理
$ssrsProxy = New-WebServiceProxy -Uri $ssrsWsdlUrl -UseDefaultCredential

# 准备报表参数(需匹配报表实际参数名称,可在SSRS报表设置中查看)
$reportParams = @(
    New-Object Microsoft.ReportingServices.ReportService2010.ParameterValue
    $reportParams[0].Name = "DomainName"  # 替换为报表的域参数名称
    $reportParams[0].Value = $targetDomain
)

# 定义导出格式为CSV
$deviceInfo = "<DeviceInfo><Format>CSV</Format></DeviceInfo>"

# 执行报表渲染并导出
try {
    $reportBinaryData = $ssrsProxy.Render(
        $reportFullPath,
        "CSV",
        $null,
        $deviceInfo,
        $reportParams,
        $null,
        $null,
        $null,
        $null,
        $null,
        $null
    )

    # 将二进制数据写入CSV文件
    [System.IO.File]::WriteAllBytes($outputCsvPath, $reportBinaryData)
    Write-Host "报表已成功导出至: $outputCsvPath"
}
catch {
    Write-Error "导出失败: $_"
}

方案二:使用SSRS REST API(适用于SSRS 2016+)

如果你的SSRS版本是2016或以上,可以用更简洁的REST API方式:

# 配置参数
$ssrsServer = "你的SSRS服务器地址"
$reportFullPath = "/ConfigMgr_BNL/Hardware/General/Computers in a specific workgroup or domain"
$targetDomain = "需要查询的域名"
$outputCsvPath = "C:\Reports\DailyComputers.csv"

# 构造API请求地址
$apiRenderUrl = "http://$ssrsServer/ReportServer/api/v2.0/Reports('$([Uri]::EscapeDataString($reportFullPath))')/Render"

# 定义请求参数
$requestParams = @{
    format = "CSV"
    DomainName = $targetDomain  # 替换为报表的域参数名称
}

# 发送请求并保存结果
Invoke-RestMethod -Uri $apiRenderUrl -Method Get -UseDefaultCredential -Body $requestParams -OutFile $outputCsvPath
Write-Host "CSV文件已导出至: $outputCsvPath"

关键注意事项

  • 参数名称确认:脚本中的DomainName需要替换为报表实际的参数名称,可在SSRS报表的「参数」设置中查看。
  • 权限配置:运行脚本的账号需要有SSRS报表的查看权限,以及导出路径的写入权限。
  • 自动化执行:将脚本保存为.ps1文件后,通过Windows任务计划程序设置每日定时执行,实现完全自动化。
  • HTTPS处理:如果SSRS使用HTTPS,需将URL改为https://,若遇到证书问题,可在开发环境临时添加-SkipCertificateCheck参数(生产环境建议配置信任证书)。

内容的提问来源于stack exchange,提问作者Toon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 19:40:26