SSRS 和 PowerShell:获取 Excel 格式的报告

2024-01-03

我正在尝试让 PowerShell 向我们的 SSRS 服务器发送 Web 请求并捕获结果。我已经使用了撞墙rs:FORMAT=EXCELSSRS url 字符串中的参数。我有以下内容:

首先,初始化凭据:

$User = "MYDOMAIN\MyUser"
$PWord = ConvertTo-SecureString -String "WooHooStringP$W0rd" -AsPlainText -Force
$c = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $PWord

现在,请求一份报告:

Invoke-WebRequest `
-UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer) `
-Credential $c `
-Uri "http://myserver/ReportServer_DEV/Pages/ReportViewer.aspx?/folder+path/report+name"

这很好用。我什至可以获取结果(附上此请求并使用 ().Content)。 然后,指定一种格式而不是纯渲染:

Invoke-WebRequest `
-UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer) `
-Credential $c `
-Uri "http://myserver/ReportServer_DEV/Pages/ReportViewer.aspx?/folder+path/report+name&rs:format=HTML4.0"

请注意rs:Format规格?奇迹般有效。

然后,为大结局,给我一个 Excel 文件:

Invoke-WebRequest `
-UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer) `
-Credential $c `
-Uri "http://myserver/ReportServer_DEV/Pages/ReportViewer.aspx?/folder+path/report+name&rs:format=EXCEL"

没办法,伙计:

Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.
At line:1 char:11
+ $bytez = (Invoke-WebRequest `
+           ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

为什么rs:format=EXCEL选项抛出未经授权的异常,而所有其他 URL 均由 SSRS 提供服务?


我已经想通了!我以错误的方式解决了这个问题:SSRS 通过 Web 服务提供访问,PowerShell 可以使用该服务,而无需破解 URL 并捕获响应。我找到了一个执行此操作的脚本并对其进行了修改以满足我的目的:

function GetRSConnection($server, $instance)
{
    #   Create a proxy to the SSRS server and give it the namespace of 'RS' to use for
    #   instantiating objects later.  This class will also be used to create a report
    #   object.

    $User = "DOMAIN\Username"
    $PWord = ConvertTo-SecureString -String "Pa$$w0rd" -AsPlainText -Force
    $c = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $PWord

    $reportServerURI = "http://" + $server + "/" + $instance + "/ReportExecution2005.asmx?WSDL"

    $RS = New-WebServiceProxy -Class 'RS' -NameSpace 'RS' -Uri $reportServerURI -Credential $c
    $RS.Url = $reportServerURI
    return $RS
}

function GetReport($RS, $reportPath)
{
    #   Next we need to load the report. Since Powershell cannot pass a null string
    #   (it instead just passses ""), we have to use GetMethod / Invoke to call the
    #   function that returns the report object.  This will load the report in the
    #   report server object, as well as create a report object that can be used to
    #   discover information about the report.  It's not used in this code, but it can
    #   be used to discover information about what parameters are needed to execute
    #   the report.
    $reportPath = "/" + $reportPath
    $Report = $RS.GetType().GetMethod("LoadReport").Invoke($RS, @($reportPath, $null))

    # initialise empty parameter holder
    $parameters = @()
    $RS.SetExecutionParameters($parameters, "nl-nl") > $null
    return $report
}

function AddParameter($params, $name, $val)
{
    $par = New-Object RS.ParameterValue
    $par.Name = $name
    $par.Value = $val
    $params += $par
    return ,$params
}

function GetReportInFormat($RS, $report, $params, $outputpath, $format)
{
    #   Set up some variables to hold referenced results from Render
    $deviceInfo = "<DeviceInfo><NoHeader>True</NoHeader></DeviceInfo>"
    $extension = ""
    $mimeType = ""
    $encoding = ""
    $warnings = $null
    $streamIDs = $null

    #   Report parameters are handled by creating an array of ParameterValue objects.
    #   Add the parameter array to the service.  Note that this returns some
    #   information about the report that is about to be executed.
    #   $RS.SetExecutionParameters($parameters, "en-us") > $null
    $RS.SetExecutionParameters($params, "nl-nl") > $null

    #    Render the report to a byte array.  The first argument is the report format.
    #    The formats I've tested are: PDF, XML, CSV, WORD (.doc), EXCEL (.xls),
    #    IMAGE (.tif), MHTML (.mhtml).
    $RenderOutput = $RS.Render($format,
        $deviceInfo,
        [ref] $extension,
        [ref] $mimeType,
        [ref] $encoding,
        [ref] $warnings,
        [ref] $streamIDs
    )

    #   Determine file name
    $parts = $report.ReportPath.Split("/")
    $filename = $parts[-1] + "."
    switch($format)
    {
        "EXCEL" { $filename = $filename + "xls" } 
        "WORD" { $filename = $filename + "doc" }
        "IMAGE" { $filename = $filename + "tif" }
        default { $filename = $filename + $format }
    }

    if($outputpath.EndsWith("\\"))
    {
        $filename = $outputpath + $filename
    } else
    {
        $filename = $outputpath + "\" + $filename
    }

    $filename

    # Convert array bytes to file and write
    $Stream = New-Object System.IO.FileStream($filename), Create, Write
    $Stream.Write($RenderOutput, 0, $RenderOutput.Length)
    $Stream.Close()
}

$RS = GetRSConnection -server "DEVBOX" -instance "ReportServer_DEV"
$report = GetReport -RS $RS -reportPath "folder name/report name"

$params = @()
$params = AddParameter -params $params -name "Month" -val "201311"

GetReportInformat -RS $RS -report $report -params $params -outputpath "i:\test" -format "EXCEL"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SSRS 和 PowerShell:获取 Excel 格式的报告 的相关文章

随机推荐

  • 共享 SwiftUI 视图的屏幕截图导致崩溃

    我正在抓取 SwiftUI 视图中子视图的屏幕截图 立即传递到共享表以共享图像 该视图是来自呈现为一堆卡片的文本数组的一组问题 我正在尝试获取问题的屏幕截图 并将其与应用程序的链接一起共享 使用愤怒的小鸟的链接进行测试 我基本上可以使用 A
  • 读取实例导致解析错误

    我想实现一个 read 实例 使我能够读取字符串 例如 8 3 并构造一个包含这些值的列表 data Value a Nul Val a showsValue Show a gt Value a gt ShowS showsValue Va
  • 在 Azure 数据工厂中引用 JSON 有效负载值作为 If 条件

    我有一个像这样的 Json 文件作为从 API 调用返回的有效负载 这是数据工厂中的 http 数据集类型 count 2 name DatasetABC columnNames Column 1 Column 2 rows 1234 56
  • AWS CLI 上传失败:未知编码:idna

    我尝试使用 AWS CLI 将一些文件推送到 s3 但遇到错误 upload failed An HTTP Client raised and unhandled exception unknown encoding idna 我相信这是一
  • Python 包及其对应的 PyPi 项目可以有不同的名称吗?

    例如 我想知道怎么可能scikit learn是 PyPi 包的名称 而实际的 Python 模块的名称是sklearn 我问的原因是我有一个本地Python包packageA我无法上传到 PyPi 因为该名称恰好已被占用 因此我想知道我是
  • 如何在Delphi中实现一套标准的超链接检测规则

    我目前在程序中自动检测文本内的超链接 我把它做得非常简单 只需要寻找http or www 然而 一位用户建议我将其扩展到其他形式 例如 https or com 然后我意识到它可能不止于此 因为还有 ftp mailto 和 file 所
  • 每个应用程序的长路径行为是否可以通过清单启用?

    尽管 MSDN 文档是什么say https msdn microsoft com en us library windows desktop aa365247 aspx maxpath 您还可以通过以下方式启用每个应用程序的新长路径行为
  • R:如何检查可用的核心数/CPU 使用率

    R 是单线程的 使用 R 如何检查 Windows 和 Linux 中运行 R 的核心 线程数 或者正在运行多少卢比 使用R 如何检查Windows和Linux中运行R的每个核心的使用情况 或者每个 R 使用的 CPU 百分比 例如 如果我
  • IIS 7.5 在哪里记录错误?

    IIS 7 5 在哪里记录错误 事件查看器 日志档案 我收到一个非常不具体的内部 500 错误 我想了解更多 我正在运行 PHP 我做了什么最后一条评论 http forums iis net t 1169733 aspx在这个帖子上说 但
  • 检测直角三角形的C程序

    如果给我坐标系中的 100 个点 我必须找出这些顶点中是否存在直角三角形 有没有一种方法可以检测这些顶点中的直角三角形 而不必选择所有 3 个顶点对 然后对它们应用毕达哥拉斯定理 有更好的算法吗 谢谢你的帮助 这是一个O n 2 log n
  • 对绑定到 DataTable 的 GridView 进行排序

    我有一个存储库 其中包含我正在处理的这个项目的所有 LINQ 查询 我能够将 LINQ 结果获取到 DataTable 并将其绑定到 gridview 以显示数据 现在我需要使 gridview 可排序 我已经设定AllowSorting
  • 如何确定 Git 中合并提交的第一个父级

    我正在阅读有关使用的差异 vs git 中的运算符 我遇到了这个问题Git 中的 HEAD 和 HEAD 有什么区别 https stackoverflow com q 2221658 2498327 在谷歌搜索后 我在网上找不到一个很好的
  • 当我有触摸事件处理程序时,为什么我的鼠标事件处理程序不工作?

    我的一些最终用户拥有触摸屏 其他用户则拥有 PC 在触摸屏上 PreviewMouseUp Down与触摸事件处理程序一起触发 导致重复行为 在 PreviewMousUp Down 中编写的函数被执行两次 所以我的示例按钮 XAML
  • 当涉及两个输入时,jQuery 模糊不断触发

    我在 Google 和 Stack Overflow 上搜索了一段时间 但找不到与此相关的任何内容 我有多个输入文本字段 当模糊时 它们会向服务器调用 jQuery AJAX 函数 它几乎可以完美地工作 问题是 如果我专注于一个文本输入 然
  • 为什么 Set 与 Proxy 不兼容?

    JavaScriptSet https developer mozilla org en docs Web JavaScript Reference Global Objects Set似乎与 JavaScript 完全不兼容proxies
  • Pymongo 连接远程机器超时

    我有一个在 AWS EC2 上运行的 Bitnami MEAN 堆栈 我正在尝试使用 PyMongo 从远程计算机进行连接 from pymongo import MongoClient conn MongoClient mongodb u
  • Automapper:忽略条件

    是否可以根据源属性的值忽略映射成员 例如 如果我们有 public class Car public int Id get set public string Code get set public class CarViewModel p
  • 可以要求我的事件处理程序立即返回吗?

    我正在编写一个 NET 库 其中一个类包含库用户需要订阅的事件 是否可以要求这些事件的处理程序的实现快速返回 或者这是一个有共同解决方案的常见问题吗 如果处理程序花费很长时间 这不会是致命的 但如果处理程序花费的时间超过大约半秒 事情就会开
  • Angular 5 - 添加动态路由但不路由

    我创建了一个新项目 并向路由模块添加了一些代码以进行动态路由 路由模块代码如下 import NgModule from angular core import Routes RouterModule Router from angular
  • SSRS 和 PowerShell:获取 Excel 格式的报告

    我正在尝试让 PowerShell 向我们的 SSRS 服务器发送 Web 请求并捕获结果 我已经使用了撞墙rs FORMAT EXCELSSRS url 字符串中的参数 我有以下内容 首先 初始化凭据 User MYDOMAIN MyUs