Functions/CatalogItems/Remove-RsSubscription.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright (c) 2016 Microsoft Corporation. All Rights Reserved.
# Licensed under the MIT License (MIT)

function Remove-RsSubscription
{
    <#
        .SYNOPSIS
            This function removes subscription(s) associated to reports.

        .DESCRIPTION
            This function removes subscription(s) associated to reports.

        .PARAMETER Subscription
            An object returned from Get-RsSubscription that contains one or multiple SubscriptionId to remove. Do NOT specify $SubscriptionId parameter when specifying this parameter.

        .PARAMETER SubscriptionId
            The SubscriptionId to remove. Do NOT specify $Subscription parameter when specifying this parameter.

        .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
            Remove-RsSubscription -SubscriptionId 'b4694569-99a9-4cb3-bd59-7bf710b04a0c'

            Description
            -----------
            Removes the subscription with ID 'b4694569-99a9-4cb3-bd59-7bf710b04a0c' from Report Server located at http://localhost/reportserver

        .EXAMPLE
            Get-RsSubscription -ReportServerUri http://localhost/ReportServer_SQL2016 -Path '/path/to/my/report' |
            Out-GridView -PassThru |
            Remove-RsSubscription -ReportServerUri http://localhost/ReportServer_SQL2016

            Description
            -----------
            Gets all the subscriptions associated to the report located at '/path/to/my/report' found at the Report Server located at http://localhost/ReportServer_SQL2016 and displays them in a gridview. Then it tries to delete all the susbscriptions after prompting user for confirmation.

        .EXAMPLE
            $rsProxy = New-RsWebServiceyProxy -ReportServerUri http://localhost/ReportServer_SQL2016
            Get-RsSubscription -Proxy $rsProxy -Path '/path/to/my/report' |
            Out-GridView -PassThru |
            Remove-RsSubscription =Proxy $rsProxy

            Description
            -----------
            Gets all the subscriptions associated to the report located at '/path/to/my/report' found at the Report Server located at http://localhost/ReportServer_SQL2016 and displays them in a gridview. Then it tries to delete all the susbscriptions after prompting user for confirmation.
    #>


    [CmdletBinding(SupportsShouldProcess = $True, ConfirmImpact = 'High')]
    param (
        [Parameter(ParameterSetName='MutipleSubscriptions', Mandatory=$True, ValueFromPipeline=$True)]
        [object[]]
        $Subscription,

        [Parameter(ParameterSetName='SingleSubscription', Mandatory=$True)]
        [string]
        $SubscriptionId,

        [string]
        $ReportServerUri,

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

        $Proxy
    )
    Begin
    {
        $Proxy = New-RsWebServiceProxyHelper -BoundParameters $PSBoundParameters
    }
    Process
    {
        if ([System.String]::IsNullOrEmpty($SubscriptionId)) 
        {
            foreach ($item in $Subscription)
            {
                if ($PSCmdlet.ShouldProcess($item.SubscriptionId, "Delete the subscription"))
                {
                    try 
                    {
                        Write-Verbose "Deleting subscription $($item.SubscriptionId) ..."
                        $Proxy.DeleteSubscription($item.SubscriptionId)
                        Write-Verbose "Subscription deleted successfully!"
                    }
                    catch 
                    {
                        throw (New-Object System.Exception("Exception occurred while deleting subscription id '$($item.SubscriptionId)'! $($_.Exception.Message)", $_.Exception))
                    }
                }
            }
        }
        else
        {
            if ($PSCmdlet.ShouldProcess($SubscriptionId, "Delete the subscription"))
            {
                try 
                {
                    Write-Verbose "Deleting subscription $SubscriptionId..."
                    $Proxy.DeleteSubscription($SubscriptionId)
                    Write-Verbose "Subscription deleted successfully!"
                }
                catch 
                {
                    throw (New-Object System.Exception("Exception occurred while deleting subscription id '$SubscriptionId'! $($_.Exception.Message)", $_.Exception))
                }
            }
        }
    }
}