Thursday, March 31, 2016

SSRS and powershell: Parameter not accepted

Leave a Comment

I use Powershell to run several reports on Microsoft SQL Report Services and to save the results to a Word doc. I have a script with functions that handle communications with the Report Server:

## 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() } 

Then, I have a script that executes a report containing the financial quarterly data. This script runs fine:

## 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 

The values for $kwartaal and $kptc are hard-coded here, but are parameters in the actual version of this script. Besides the financial quarterly, we have three other quarterly reports that need to be output by this script. Two of these run fine, in the fourth I can't seem to get one of the parameters right. The script for that one is:

## 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 

When I execute this script, I get this error:

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. 

I clearly DO supply a value for 'HoofdspecialismeSpecialismeOms'; I've previously noticed that this error also is thrown when the parameter is not in the expected format. This format, since the report filter is based on a hierarchy in an SSAS cube, looks like this: [hierarchy].[sub-level].&[member]. I've ensured that [Hoofdspecialisme].[Specialisme Oms].&[$s] is the correct format by looking it up in the query that populates the prompt in SSRS. The report does display data when run through SSRS - and taking a parameter from the prompt.

I did notice that this parameter allows multiple selection. However, I don't believe this leads to the error because that is also true for AanvragendSpecialismeSpecialismeOms.

Any idea why this one parameter fails to be fed into the report when calling GetReportInformat?

1 Answers

Answers 1

Have you tried

function AddParameter($params, $name, $val) {     $par = New-Object RS.ParameterValue     $par.Name = $name     $par.Value = $val     $params += $par     return ,$params     #      ^Removing this comma? } 

As well as declaring the data types explicitly for your parameters?

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

Also, with so many user-defined helper functions calling imported types that call methods and set properties to a report we can't see, it can get a little difficult to help troubleshoot in-depth for this specific report you're getting an error on. It looks like you've tried moving the line around in the order which sounds to me like you might have an issue with how that specific report parses the values you input through RS.ParameterValue so maybe take a look at if it accepts the string you set in -val for your AddParameter user defined function.

Edit:

From https://social.msdn.microsoft.com/Forums/sqlserver/en-US/e38b4a34-c780-43bb-8321-15f96d0938a9/exception-calling-render-systemwebservicesprotocolssoapexception-one-or-more-data-source?forum=sqlreportingservices

This error is generated when you are attempting to run a report in which one or more of the data sources are set to "prompt" credentials. This means we do not use your Windows credentials automatically, but rather you need to supply a different set of credentials which are used only for the data source.

Sounds like you might need to put aside the script and check if the report is different.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment