【发布时间】:2016-03-22 14:02:35
【问题描述】:
我使用 Powershell 在 Microsoft SQL Report Services 上运行多个报告并将结果保存到 Word 文档。我有一个脚本,其中包含处理与报表服务器通信的函数:
## File "qrap-functions.ps1"
function GetRSConnection($server, $instance)
{
$User = "xxxx"
$PWord = ConvertTo-SecureString -String "yyyy" -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)
{
$reportPath = "/" + $reportPath
#$reportPath
$Report = $RS.GetType().GetMethod("LoadReport").Invoke($RS, @($reportPath, $null))
$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, $format, $saveas)
{
$deviceInfo = "<DeviceInfo><NoHeader>True</NoHeader></DeviceInfo>"
$extension = ""
$mimeType = ""
$encoding = ""
$warnings = $null
$streamIDs = $null
$RS.SetExecutionParameters($params, "nl-nl") > $null
$RenderOutput = $RS.Render($format,
$deviceInfo,
[ref] $extension,
[ref] $mimeType,
[ref] $encoding,
[ref] $warnings,
[ref] $streamIDs
)
$Stream = New-Object System.IO.FileStream($saveas), Create, Write
$Stream.Write($RenderOutput, 0, $RenderOutput.Length)
$Stream.Close()
}
然后,我有一个脚本来执行包含财务季度数据的报告。该脚本运行良好:
## File "qrap-financieel.ps1"
. "./qrap-functions.ps1"
$saveas = "e:\test\financieel.doc"
$RS = GetRSConnection -server "MSZRDWH" -instance "reportserver_acc"
$report = GetReport -RS $RS -reportPath "kwartaalrapportage/kwartaalrapportage financieel"
$params = @()
$kwartaal = "[Periode Maand].[Jaar Kwartaal].&[2015-2]"
$kptc = "[Kostenplaats].[Team code].&[2003]"
$params = AddParameter -params $params -name "PeriodeMaandJaarKwartaal" -val $kwartaal
$params = AddParameter -params $params -name "KostenplaatsTeamcode" -val $kptc
GetReportInformat -RS $RS -report $report -params $params -format "WORD" -saveas $saveas
$kwartaal 和 $kptc 的值在此处是硬编码的,但在此脚本的实际版本中是参数。除了财务季度,我们还有其他三个季度报告需要通过此脚本输出。
其中两个运行良好,在第四个中我似乎无法正确设置其中一个参数。该脚本是:
## File "qrap-zorglog.ps1"
. "./qrap-functions.ps1"
$RS = GetRSConnection -server "MSZRDWH" -instance "reportserver_acc"
$report = GetReport -RS $RS -reportPath "kwartaalrapportage/kwartaalrapportage zorglogistiek"
$s = "Urologie"
$saveas = "e:\test\ZL Urologie.doc"
$params = @()
$kwartaal = "[Periode Maand].[Jaar Kwartaal].&[2015-2]"
$params = AddParameter -params $params -name "HoofdspecialismeSpecialismeOms" -val "[Hoofdspecialisme].[Specialisme Oms].&[$s]"
$params = AddParameter -params $params -name "PeriodeMaandJaarKwartaal" -val $kwartaal
$params = AddParameter -params $params -name "WachttijdenSpecialismeSpecialisme" -val "[Wachttijden Specialisme].[Specialisme].&[$s]"
$params = AddParameter -params $params -name "SpecialisatieGroeperingSpecialisatieGroeperingOms" -val "[Specialistie Groepering].[Specialistie Groepering Oms].&[$s]"
$params = AddParameter -params $params -name "AanvragendSpecialismeSpecialismeOms" -val "[AanvragendSpecialisme].[Specialisme Oms].&[$s]"
GetReportInformat -RS $RS -report $report -params $params -format "WORD" -saveas $saveas
当我执行这个脚本时,我得到这个错误:
Exception calling "Render" with "7" argument(s): "System.Web.Services.Protocols.SoapException: This report requires a
default or user-defined value for the report parameter 'HoofdspecialismeSpecialismeOms'. To run or subscribe to this
report, you must provide a parameter value. ---> Microsoft.ReportingServices.Diagnostics.Utilities.ReportParameterValueNot
SetException: This report requires a default or user-defined value for the report parameter
'HoofdspecialismeSpecialismeOms'. To run or subscribe to this report, you must provide a parameter value.
我显然确实为“HoofdspecialismeSpecialismeOms”提供了一个值;我之前注意到,当参数不是预期的格式时,也会引发此错误。这种格式,由于
报告过滤器基于 SSAS 多维数据集中的层次结构,如下所示:[hierarchy].[sub-level].&[member]。我已确保 [Hoofdspecialisme].[Specialisme Oms].&[$s] 是正确的格式
在填充 SSRS 提示的查询中查找它。报表在通过 SSRS 运行时会显示数据 - 并从提示中获取参数。
我确实注意到这个参数允许多选。但是,我不认为这会导致错误,因为 AanvragendSpecialismeSpecialismeOms 也是如此。
知道为什么在调用GetReportInformat 时无法将这个参数输入到报告中吗?
【问题讨论】:
-
我对 Powershell 的工作不多,但您是否尝试过参数之间没有多余的空格?它是用空格分隔的。
-
@HannoverFist 我刚刚尝试在
-name和-val参数之间没有额外空格和制表符的情况下运行脚本,但没有运气。我仍然得到同样的错误。按参数顺序移动HoofdspecialismeSpecialismeOms也不会改变。 -
@Matt 你说得对
$q。在我的脚本中,这是一个参数。 Write-Host 告诉我这完全按照我的意愿得到解决。我已编辑问题以修复此值。该脚本确实有一个$s的定义,但它是“Urologie”。 -
如果更改参数顺序,该条目是否仍会显示错误?
-
@Matt 是的,更改顺序对错误消息没有影响。我知道参数之间可能存在交叉依赖关系,但这不适用于此处。
标签: powershell reporting-services