使用PowerShell创建SSRS数据驱动订阅

时间:2017-01-21 15:27:17

标签: c# powershell reporting-services

编辑:将整个已转换的c#添加到PowerShell并发出问题

我正在尝试将MSDN C#代码转换为PowerShell脚本。 这是MSDN link

我几乎完成了转换,但是我的最后一行遇到了一些问题。

这是完整的错误消息:使用“7”参数调用“CreateDataDrivenSubscription”的异常:“生成XML文档时出错。”

以下是创建数据驱动订阅的第7个参数的代码段。

这是PowerShell脚本:

$Parameters | Add-Member -type NoteProperty -Name $EMPID.ParameterName -Value $EMPID.FieldAlias

$ReportYear = New-Object $ParameterValue
$ReportYear.Name = "ReportYear"
$ReportYear.Value = "2017"

$Parameters | Add-Member -type NoteProperty -Name $ReportYear.Name -Value $ReportYear.Value

$ReportMonth = New-Object $ParameterValue

$ReportMonth.Name = "ReportMonth"
$ReportMonth.Value = "6"

$Parameters | Add-Member -Type NoteProperty -Name $ReportMonth.Name -Value $ReportMonth.Value


$SubscriptionID = $objWebServerProxy.CreateDataDrivenSubscription($Report,$Settings,$arrDataRetrievalPlan,$Description,$EventType,$MatchData,$Parameters)

这是C#代码

ParameterValueOrFieldReference[] parameters = new ParameterValueOrFieldReference[3];

ParameterFieldReference empID = new ParameterFieldReference(); // Data-driven.
empID.ParameterName = "EmpID";
empID.FieldAlias = "EmpID";
parameters[0] = empID;

ParameterValue reportYear = new ParameterValue();
reportYear.Name = "ReportYear";
reportYear.Value = "2004";
parameters[1] = reportYear;

ParameterValue reportMonth = new ParameterValue();
reportMonth.Name = "ReportMonth";
reportMonth.Value = "6"; // June
parameters[2] = reportMonth;

try
{
    string subscriptionID = rs.CreateDataDrivenSubscription( 
report, settings, dataRetrieval, description, eventType, matchData, parameters);

你能告诉我在转换中的哪个方面错过了吗?

这是我整个转换过的脚本。

$URI = "URI"

$objWebServerProxy = New-WebServiceProxy -Uri $URI -UseDefaultCredential -ErrorAction Stop

$Namespace = $objWebServerProxy.GetType().Namespace
$ExtensionSettigs = ($Namespace + '.ExtensionSettings')
$ParameterValueOrFieldReference = ($Namespace + '.ParameterValueOrFieldReference')
$ParameterFieldReference = ($Namespace + '.ParameterFieldReference')
$ParameterValue = ($Namespace + '.ParameterValue')
$Datasource = ($Namespace + '.DataSource')
$DataSourceDefinition = ($Namespace + '.DataSourceDefinition')
$Field = ($Namespace + '.Field')
$CredentialRetrievalEnum = ($Namespace + '.CredentialRetrievalEnum')
$DataSetDefinition = ($Namespace + '.DataSetDefinition')
$QueryDefinition = ($Namespace + '.QueryDefinition')
$DataSetDefinition = ($Namespace +'.DataSetDefinition')
$DataRetrievalPlan = ($Namespace+'.DataRetrievalPlan')
$ScheduleReference = ($Namespace+'.ScheduleReference')

$NameSharedSchedule="Mike_Test";
$NeverExpireSchedule= $objWebServerProxy.ListSchedules([System.Management.Automation.Language.NullString]::Value) | where {$_.Name -eq "$NameSharedSchedule"}
$NeverExpireScheduleID = $NeverExpireSchedule.scheduleid;
$NeverExpireDescription = $NeverExpireSchedule.Description;
$NeverExpireDefinition = $NeverExpireSchedule.Definition;
#Write-Host "Found Shared Schedule: '$NameSharedSchedule' with id $NeverExpireScheduleID and definition $NeverExpireDescription";

$NeverExpireScheduleRef =New-Object $ScheduleReference
$NeverExpireScheduleRef.ScheduleID=$NeverExpireScheduleID;

#$Report = "/ForMigration/Subscription_Test/SSRSRationalization_ExecutionLog_LastEightDays"
$Report = "/Gayatree/GTest/ExecutionLog_LastEightDays"
$Description = "My new Data Driven Subscription"

$Settings = New-Object $ExtensionSettigs

$Settings.Extension = "Report Server Email";

$ExtensionParams = New-Object $ParameterValueOrFieldReference

#$ExtensionParams.ParameterValueOrFieldReference ='8'



$To = New-Object $ParameterFieldReference 
$To.ParameterName = "TO"
$To.FieldAlias = "Email"

$ExtensionParams | Add-Member -type NoteProperty -name $To.ParameterName -Value $To.FieldAlias

$ReplyTo = New-Object $ParameterValue
$ReplyTo.Name = "ReplyTo"
$ReplyTo.Value = "Email"

$ExtensionParams | Add-Member -type NoteProperty -name $ReplyTo.Name -Value $ReplyTo.Value

$IncludeReport = New-Object $ParameterValue
$IncludeReport.Name = "IncludeReport"
$IncludeReport.Value = $false

$ExtensionParams | Add-Member -type NoteProperty -name $IncludeReport.Name -Value $IncludeReport.Value

$RenderFormat = new-object $ParameterValue
$RenderFormat.Name = "ReportFormat";
$RenderFormat.Value = "HTML4.0"

$ExtensionParams | Add-Member -type NoteProperty -name $RenderFormat.Name -Value $RenderFormat.Value

$Priority = New-Object $ParameterValue
$Priority.Name = "Priority"
$Priority.Value = "Normal"

$ExtensionParams | Add-Member -type NoteProperty -name $Priority.Name -Value $Priority.Value

$Subject = New-Object $ParameterValue
$Subject.Name = "Subject"
$Subject.Value = "Your sales report"
$ExtensionParams | Add-Member -type NoteProperty -name $Subject.Name -Value $Subject.Value

$Comment = New-Object $ParameterValue
$Comment.Name = "Comment"
$Comment.Value = "Here is the link to your report."

$ExtensionParams | Add-Member -type NoteProperty -name $Comment.Name -Value $Comment.Value 

$IncludeLink = New-Object $ParameterValue
$IncludeLink.Name = "IncludeLink";
$IncludeLink.Value = $true

$ExtensionParams | Add-Member -Type NoteProperty -name $IncludeLink.Name -Value $IncludeLink.Value

$Settings.ParameterValues = $ExtensionParams



$DSName = New-Object $Datasource
$DSName.Name = "Mike_Test"

<#
$DSDefinition = New-Object $DataSourceDefinition
$CredentialRetrieval = New-Object $CredentialRetrievalEnum
$CredentialRetrieval.value__ = 1

$DSDefinition.ConnectString = "Data source = server;Initial Catalog = ReportServer"
$DSDefinition.CredentialRetrieval = $CredentialRetrieval
$DSDefinition.Enabled = $true
$DSDefinition.EnabledSpecified = $true
$DSDefinition.Extension = "SQL"
$DSDefinition.ImpersonateUserSpecified = $false
$DSDefinition.UserName = "Username"
$DSDefinition.Password = "Password"

#>
$DS = $objWebServerProxy.ListChildren('/',$false)|Where-Object {$_.TypeName -eq "DataSource"}

   foreach($DSS in $DS){
   $anotherPass = $DSS.path
$another = $objWebServerProxy.GetDataSourceContents("$anotherPass")
$another.Password = "TestPass"
}
$another
$DSName.Item = $another

$FieldList = New-Object $Field
$FieldList.Name = "EmailAddress"
$FieldList.Alias = "EmailAddress"

$FieldList | Add-Member -Type NoteProperty -name $FieldList.Name -Value $FieldList.Alias

$FieldList.Name = "EmpID"
$FieldList.Alias = "EmpID"

$FieldList | Add-Member -Type NoteProperty -name $FieldList.Name -Value $FieldList.Alias


$DataSetDef = New-Object $DataSetDefinition

$DataSetDef.AccentSensitivitySpecified = $false
$DataSetDef.CaseSensitivitySpecified = $false
$DataSetDef.KanatypeSensitivitySpecified = $false
$DataSetDef.WidthSensitivitySpecified = $false
$DataSetDef.Fields = $FieldList


$arrQueryDefinition = New-Object $QueryDefinition
$arrQueryDefinition.CommandText = "Select getdate()"
$arrQueryDefinition.CommandType = "Text"
$arrQueryDefinition.Timeout = '45'
$arrQueryDefinition.TimeoutSpecified = $true
$DataSetDef.Query = $arrQueryDefinition
$Results = New-Object $DataSetDefinition
$Bool = $true
$Name = "Parameter"
$DSDefinition

$DataSetDef


$DSName
$Results = $objWebServerProxy.PrepareQuery($DSName,$DataSetDef,[ref]$Bool,[ref]$Name)

$arrDataRetrievalPlan = New-Object $DataRetrievalPlan
$arrDataRetrievalPlan.DataSet = $Results
$arrDataRetrievalPlan.Item = $another

$EventType = "TimedSubscription";
$MatchData = $NeverExpireScheduleID

$Parameters = New-Object $ParameterValueOrFieldReference
#
$EMPID = New-Object $ParameterFieldReference
$EMPID.ParameterName = "EmpID"
$EMPID.FieldAlias = "EmpID"

$Parameters | Add-Member -type NoteProperty -Name $EMPID.ParameterName -Value $EMPID.FieldAlias

$ReportYear = New-Object $ParameterValue
$ReportYear.Name = "ReportYear"
$ReportYear.Value = "2017"

$Parameters | Add-Member -type NoteProperty -Name $ReportYear.Name -Value $ReportYear.Value

$ReportMonth = New-Object $ParameterValue

$ReportMonth.Name = "ReportMonth"
$ReportMonth.Value = "6"

$Parameters | Add-Member -Type NoteProperty -Name $ReportMonth.Name -Value $ReportMonth.Value



$SubscriptionID = $objWebServerProxy.CreateDataDrivenSubscription($Report,$Settings,$arrDataRetrievalPlan,$Description,$EventType,$MatchData,$Parameters)

1 个答案:

答案 0 :(得分:0)

我不确定您创建ParameterValue实例的方式是否有效 - 由于$ ParameterValue声明丢失,我无法确定,但这可能会导致问题。 您可能希望尝试更多内容:

$reportServerUri = "http://localhost/reportserver/ReportService2010.asmx?wsdl"
$rs = New-WebServiceProxy -Uri $reportServerUri -UseDefaultCredential -Namespace "SSRS"

# Note the difference, here we use the type generated when creating the webservice proxy:
$ReportYear = New-Object SSRS.ParameterValue 
$ReportYear.Name = "ReportYear"
$ReportYear.Value = "2017"

请参阅我的blog post here以获取快速教程。

修改 要确定错误CreateDataDrivenSubscription的哪个参数,请尝试用$null替换一个或多个参数 - 根据方法的文档确定它可能无效,但只有在呼叫已成功发送到服务器并反序列化。

换句话说,如果用$Parameters替换$null参数不会抛出生成XML文档&#34;的错误,那么错误就在$Parameters参数中:

$SubscriptionID = $objWebServerProxy.CreateDataDrivenSubscription($Report,$Settings,
           $arrDataRetrievalPlan,$Description,$EventType,$MatchData,$null)