Skip to content

Commit

Permalink
Pashah/subscriptions (#125)
Browse files Browse the repository at this point in the history
* Adding support to New-RsSubscription

* Renaming Set-RsSubscription to Copy-RsSubscription

* Test code cleanup

* Formatting changes to Import-RsSubscriptionXml.

* Minor changes to Get-RsSubscription

* Changed ConfirmImpact for Remove-RsSubscription and Remove-RsCatalogItem to High.

* Renamed Update-RsSubscription to Set-RsSubscription

* Updating help menu.
  • Loading branch information
parthsha authored Dec 20, 2017
1 parent 2bc8bda commit e5e0323
Show file tree
Hide file tree
Showing 36 changed files with 1,342 additions and 1,954 deletions.
153 changes: 153 additions & 0 deletions ReportingServicesTools/Functions/CatalogItems/Copy-RsSubscription.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Copyright (c) 2016 Microsoft Corporation. All Rights Reserved.
# Licensed under the MIT License (MIT)

function Copy-RsSubscription
{
<#
.SYNOPSIS
This script creates a new reporting subscription using an existing subscription.
.DESCRIPTION
This script creates a new reporting subscription based on the info of an existing subscription (retrieved using Get-RsSubscription).
You can choose a specific report or pass a folder. When using a folder, the report must have the same name.
NOTE: A new subscriptionId will be generated.
.PARAMETER RsItem
Specify the path to the destination report. Can't be used with -RsFolder parameter.
.PARAMETER RsFolder
Specify the folder where the destination reports exists. Can't be used with -RsItem parameter.
.PARAMETER Subscription
A object with all subscritpion configurations. The default output of Get-RsSubscription. You must piping it to this command.
.PARAMETER ReportServerUri
Specify the Report Server URL to your SQL Server Reporting Services Instance.
Use the "Connect-RsReportServer" function to set/update a default value.
.PARAMETER Credential
Specify the credentials to use when connecting to the Report Server.
Use the "Connect-RsReportServer" function to set/update a default value.
.PARAMETER Proxy
Report server proxy to use.
Use "New-RsWebServiceProxy" to generate a proxy object for reuse.
Useful when repeatedly having to connect to multiple different Report Server.
.EXAMPLE
Get-RsSubscription -ReportServerUri 'http://localhost/ReportServer_sql14' -RsItem '/path/to/my/oldReport' | Copy-RsSubscription -ReportServerUri 'http://remote-machine:8080/reportserver_sql16' -RsItem '/path/to/newReport'
Description
-----------
This command will establish a connection to the Report Server located at http://localhost/ReportServer_sql14 using current user's credentials get all subscriptions from report '/path/to/my/oldReport'
and pipe the results to Copy-RsSubscription which will create a new subscription on report '/path/to/newReport' located at Report Server 'http://remote-machine:8080/reportserver_sql16'
.EXAMPLE
Get-RsSubscription -ReportServerUri 'http://localhost/ReportServer_sql14' -RsItem '/path/to/my/oldReport' | Copy-RsSubscription -ReportServerUri 'http://remote-machine:8080/reportserver_sql16' -RsFolder '/New Folder'
Description
-----------
This command will establish a connection to the Report Server located at http://localhost/ReportServer_sql14 using current user's credentials get all subscriptions from report '/path/to/my/oldReport'
and pipe the results to Copy-RsSubscription which will create a new subscription on each report that exists with the same name on the destination folder '/New Folder' located at Report Server 'http://remote-machine:8080/reportserver_sql16'
.EXAMPLE
$paths = Get-RsCatalogItems -ReportServerUri 'http://localhost/ReportServer_sql14' -RsFolder /Origin | Where-Object TypeName -eq "Report" | Select-Object -ExpandProperty Path
Get-RsSubscription -ReportServerUri 'http://localhost/ReportServer_sql14' RsItem $paths | Copy-RsSubscription -ReportServerUri 'http://remote-machine:8080/reportserver_sql16' -RsFolder '/New Folder'
Description
-----------
This command will establish a connection to the Report Server located at http://localhost/ReportServer_sql14 using current user's credentials get all the paths from all reports at '/Origin' folder.
Then it uses the $paths variable to get all existing subscriptions and pipe the results to Copy-RsSubscription which will create a new subscription on each report that exists with the same name on the destination folder '/New Folder' located at Report Server 'http://remote-machine:8080/reportserver_sql16'
#>

[cmdletbinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
param
(
[string]
$ReportServerUri,

[System.Management.Automation.PSCredential]
$Credential,

$Proxy,

[Alias('ReportPath','ItemPath','Path')]
[Parameter(ParameterSetName='Report', Mandatory=$True)]
[string]
$RsItem,

[Alias('Folder')]
[Parameter(ParameterSetName='Folder', Mandatory=$True)]
[string]
$RsFolder,

[Parameter(Mandatory = $True, ValueFromPipeline=$true)]
[object[]]
$Subscription
)
Begin
{
$Proxy = New-RsWebServiceProxyHelper -BoundParameters $PSBoundParameters
}
Process
{
#region Input Validation
$itemNullOrEmpty = [System.String]::IsNullOrEmpty($RsItem)
$folderNullOrEmpty = [System.String]::IsNullOrEmpty($RsFolder)
if ($itemNullOrEmpty -and $folderNullOrEmpty)
{
throw 'No folder or report path was specified! You need to specify -RsFolder or -RsItem.'
}
elseif (!$itemNullOrEmpty -and !$folderNullOrEmpty)
{
throw 'Both folder and report path were specified! Please specify either -RsFolder or -RsItem.'
}
#endregion Input Validation

try
{
foreach ($sub in $Subscription)
{
if ($RsFolder)
{
$RsItem = "$RsFolder/$($sub.Report)"
}
else
{
$RsFolder = (Split-Path $RsItem -Parent).Replace("\", "/")
}

Write-Verbose "Validating if target report exists..."
if (((Get-RsFolderContent -Proxy $Proxy -RsFolder $RsFolder | Where-Object Path -eq $RsItem).Count) -eq 0)
{
Write-Warning "Can't find the report $RsItem. Skipping."
Continue
}

if ($PSCmdlet.ShouldProcess($RsItem, "Creating new subscription"))
{
Write-Verbose "Creating Subscription..."
if ($subscription.IsDataDriven)
{
$subscriptionId = $Proxy.CreateDataDrivenSubscription($RsItem, $sub.DeliverySettings, $sub.DataRetrievalPlan, $sub.Description, $sub.EventType, $sub.MatchData, $sub.Values)
}
else
{
$subscriptionId = $Proxy.CreateSubscription($RsItem, $sub.DeliverySettings, $sub.Description, $sub.EventType, $sub.MatchData, $sub.Values)
}

[pscustomobject]@{
NewSubscriptionId = $subscriptionId
DestinationReport = $RsItem
OriginalReport = $sub.Path
}
Write-Verbose "Subscription created successfully! Generated subscriptionId: $subscriptionId"
}
}
}
catch
{
throw (New-Object System.Exception("Exception occurred while creating subscription! $($_.Exception.Message)", $_.Exception))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ function Get-RsSubscription
<#
.SYNOPSIS
This script retrieves information about subscriptions for a report.
.DESCRIPTION
This script retrieves information about subscriptions for a report.
.PARAMETER Path
.PARAMETER RsItem
Specify the path to the report.
.PARAMETER ReportServerUri
Specify the Report Server URL to your SQL Server Reporting Services Instance.
Use the "Connect-RsReportServer" function to set/update a default value.
Expand All @@ -21,24 +21,31 @@ function Get-RsSubscription
The version of the API to use, 2010 by default. Sepcifiy '2005' if you need
to query a Sql Server Reporting Service Instance running a version prior to
SQL Server 2008 R2.
.PARAMETER Credential
Specify the credentials to use when connecting to the Report Server.
Use the "Connect-RsReportServer" function to set/update a default value.
.PARAMETER Proxy
Report server proxy to use.
Use "New-RsWebServiceProxy" to generate a proxy object for reuse.
Useful when repeatedly having to connect to multiple different Report Server.
.EXAMPLE
Get-RsSubscription -Path '/path/to/my/report'
Get-RsSubscription -RsItem '/path/to/my/report'
Description
-----------
This command will establish a connection to the Report Server located at http://localhost/reportserver using current user's credentials and retrieve details of subscriptions found at '/path/to/my/report'.
.EXAMPLE
Get-RsSubscription -ReportServerUri 'http://remote-machine:8080/reportserver_sql16' -RsItem '/path/to/my/report'
Description
-----------
This command will establish a connection to the Report Server located at http://remote-machine:8080/reportserver_sql16 using current user's credentials and retrieve details of subscriptions found at '/path/to/my/report'.
.EXAMPLE
Get-RsSubscription -ReportServerUri 'http://remote-machine:8080/reportserver_sql16' -Path '/path/to/my/report'
$rsProxy = New-RsWebServiceProxy -ReportServerUri 'http://remote-machine:8080/reportserver_sql16'
Get-RsSubscription -Proxy $rsProxy -RsItem '/path/to/my/report'
Description
-----------
This command will establish a connection to the Report Server located at http://remote-machine:8080/reportserver_sql16 using current user's credentials and retrieve details of subscriptions found at '/path/to/my/report'.
Expand All @@ -47,41 +54,41 @@ function Get-RsSubscription
[cmdletbinding()]
param
(
[Alias('Path')]
[Parameter(Mandatory = $True, ValueFromPipeline = $true)]
[string[]]
$Path,
$RsItem,

[string]
$ReportServerUri,

[ValidateSet('2005','2006','2010')]
[string]
$ApiVersion = '2010',

[System.Management.Automation.PSCredential]
$Credential,

$Proxy
)

Begin
{
$Proxy = New-RsWebServiceProxyHelper -BoundParameters $PSBoundParameters
}
Process
{
foreach ($item in $Path)
foreach ($item in $RsItem)
{
try
{
Write-Verbose "Retrieving subscriptions contents..."
if ($Proxy.Url -match 'ReportService2005.asmx')

if ($Proxy.Url -match 'ReportService2005.asmx')
{
if ($item -eq '/') { $item = $null }
$subscriptions = $Proxy.ListSubscriptions($Item,$null)
}
else
}
else
{
$subscriptions = $Proxy.ListSubscriptions($Item)
}
Expand All @@ -92,8 +99,8 @@ function Get-RsSubscription
$DataRetrievalPlanDataType = "$namespace.DataRetrievalPlan"
$ExtensionSettingsDataType = "$namespace.ExtensionSettings"
$ActiveStateDataType = "$namespace.ActiveState"
foreach ($subscription in $subscriptions)

foreach ($subscription in $subscriptions)
{
$extSettings = $null
$DataRetrievalPlan = $null
Expand All @@ -107,7 +114,7 @@ function Get-RsSubscription

try
{
Write-Verbose "Retrieving subscription properties for $($subscription.SubscriptionID)"
Write-Verbose "Retrieving subscription properties for $($subscription.SubscriptionID)..."

if ($subscription.IsDataDriven)
{
Expand All @@ -117,7 +124,9 @@ function Get-RsSubscription
{
$null = $Proxy.GetSubscriptionProperties($subscription.SubscriptionID, [ref]$extSettings, [ref]$desc, [ref]$active, [ref]$status, [ref]$eventType, [ref]$matchData, [ref]$values)
}


Write-Verbose "Subscription properties for $($subscription.SubscriptionID) retrieved successfully!"

#Set ExtensionSetting/s
$ExtensionSettings = New-Object $ExtensionSettingsDataType
$ExtensionSettings.Extension = $subscription.DeliverySettings.Extension
Expand All @@ -135,7 +144,7 @@ function Get-RsSubscription
$ActiveState.InvalidParameterValueSpecified = $subscription.Active.InvalidParameterValueSpecified
$ActiveState.UnknownReportParameter = $subscription.Active.UnknownReportParameter
$ActiveState.UnknownReportParameterSpecified = $subscription.Active.UnknownReportParameterSpecified

$Result = @{
SubscriptionID = $subscription.SubscriptionID
Owner = $subscription.Owner
Expand All @@ -155,9 +164,10 @@ function Get-RsSubscription
MatchData = $matchData
Values = $values
}
if ($subscription.IsDataDriven)
{
$Result.Add('DataRetrievalPlan',$DataRetrievalPlan)

if ($subscription.IsDataDriven)
{
$Result.Add('DataRetrievalPlan',$DataRetrievalPlan)
}

[pscustomobject]$Result
Expand Down
Loading

0 comments on commit e5e0323

Please sign in to comment.