简体   繁体   English

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

[英]Create SSRS Data Driven Subscription using PowerShell

Edit: Added the whole converted c# to PowerShell with issue编辑:将整个转换后的 c# 添加到 PowerShell 有问题

I'm trying to convert MSDN C# code to PowerShell script.我正在尝试将 MSDN C# 代码转换为 PowerShell 脚本。 Here's the MSDN link这是 MSDN 链接

I almost finished the conversion but I encounter some issues for my last line.我几乎完成了转换,但我的最后一行遇到了一些问题。

This is the complete error message: Exception calling "CreateDataDrivenSubscription" with "7" argument(s): "There was an error generating the XML document."这是完整的错误消息:使用“7”个参数调用“CreateDataDrivenSubscription”的异常:“生成 XML 文档时出错。”

Below is the code snippet for the 7th argument in creating data driven subscription.下面是创建数据驱动订阅的第 7 个参数的代码片段。

Here's the PowerShell script:这是 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)

Here's the C# code这是 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);

Can you tell me where did I miss on the conversion?你能告诉我我在哪里错过了转换吗?

Here's my whole converted script.这是我整个转换后的脚本。

$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)

I'm not sure if the way you're creating the ParameterValue instances works - I can't tell for sure since the $ParameterValue declaration is missing, but chances are this causes the problem.我不确定您创建 ParameterValue 实例的方式是否有效 - 我无法确定,因为缺少 $ParameterValue 声明,但很可能这会导致问题。 You might want to try something more along the lines of this:您可能想尝试更多类似的方法:

$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"

See my blog post here for a quick tutorial.有关快速教程,请参阅我的博客文章

Edit To pinpoint which argument of CreateDataDrivenSubscription the error is, try substituting one or more arguments with $null - sure it might be invalid according to the method's documentation, but the null checks only occur once the call has been successfully sent to the server and deserialized.编辑要查明错误是CreateDataDrivenSubscription的哪个参数,请尝试用$null替换一个或多个参数 - 根据方法文档确定它可能无效,但只有在调用成功发送到服务器并反序列化后才会进行空检查.

In other words, if substituting the $Parameters argument with $null does not throw the "error generating XML document", then the error is in the $Parameters argument:换句话说,如果用$null替换$Parameters参数不会抛出“错误生成 XML 文档”,那么错误在$Parameters参数中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM