使用PowerShell从SSRS报告创建Excel文件

ddarikpa  于 2023-05-17  发布在  Shell
关注(0)|答案(1)|浏览(134)

我想运行.ps1脚本,从SSRS 2017中已发布的报告自动生成Excel文件。
以下是我的脚本:

$reportServerURI = "http://NOCSQL05:80/ReportServer/"
$reportPath = "/Folder/Project"
$format = "EXCELOPENXML"
$parameters = @()

# Update these variables with the correct credentials
$username = "ValidUsr"
$password = "ValidPwd"

$RS = New-WebServiceProxy -Class 'ReportingService2010' -NameSpace 'Microsoft.SqlServer.ReportingServices2017' -Uri $reportServerURI

if($RS -ne $null) {
    $RS.Credentials = New-Object System.Net.NetworkCredential($username, $password)
    $Report = $RS.LoadReport($reportPath, $null)
    if($Report -ne $null) {
        $RS.SetExecutionParameters($parameters, "nl-nl") > $null
        $RenderOutput = $RS.Render($format, $null, [ref] $null, [ref] $null, [ref] $null, [ref] $null, [ref] $null)
        if($RenderOutput -ne $null) {
            $Stream = New-Object System.IO.FileStream("C:\Users\TM0658\Documents\report.xlsx", [System.IO.FileMode]::Create)
            $Stream.Write($RenderOutput, 0, $RenderOutput.Length)
            $Stream.Close()
        }
    }
}

我从服务器配置管理器中确认服务器URL是有效的,并且用户具有权限。
但我还是得到了这个:

New-WebServiceProxy : The request failed with HTTP status 401: Unauthorized.
At C:\Users\TM0658\Documents\SSRS_Export.ps1:11 char:7
+ $RS = New-WebServiceProxy -Class 'ReportingService2010' -NameSpace 'M ...
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (http://nocsql05/ReportServer/:Uri) [New-WebServiceProxy], WebException
    + FullyQualifiedErrorId : WebException,Microsoft.PowerShell.Commands.NewWebServiceProxy

我没有在网上找到解决方案,所有类似的脚本都是15年前的,
那么,是否有人实现了从SSRS 2017中发布的报告创建Excel文件?如果您有其他解决方案而不是PowerShell,也许它可以做到这一点

vngu2lb8

vngu2lb81#

我通过删除这一行解决了这个问题

$RS.Credentials = New-Object System.Net.NetworkCredential($username, $password)

并修改“New-WebServiceProxy”

$RS = New-WebServiceProxy -Uri $reportServerURI -Class 'ReportingService2010' -NameSpace 'Microsoft.SqlServer.ReportingServices2017' -UseDefaultCredential

相关问题