Functions/CatalogItems/Set-RsDataSetReference.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
# Copyright (c) 2016 Microsoft Corporation. All Rights Reserved.
# Licensed under the MIT License (MIT)


function Set-RsDataSetReference
{
    <#
        .SYNOPSIS
            Overrides the reference of a report to a shared dataset.
        
        .DESCRIPTION
            Overrides the reference of a report to a shared dataset.
        
        .PARAMETER Path
            Path of the report.
        
        .PARAMETER DataSetName
            Name of the dataset reference to override.
        
        .PARAMETER DataSetPath
            Path to the shared dataset the reference will point to.
        
        .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
            Set-RsDataSetReference -ReportServerUri 'http://localhost/reportserver_sql2012' -Path /ReportWithDSReference -DataSetName Dataset1 -DataSetPath /Datasets/SampleSet
            
            Description
            -----------
            Sets the dataset reference 'DataSet1' of report /ReportWithDSReference to point to dataset '/DataSets/SampleSet'
    #>

    [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
    param (
        [Alias('ItemPath')]
        [Parameter(Mandatory = $true)]
        [string[]] 
        $Path,

        [Parameter(Mandatory = $true)]
        [string]
        $DataSetName,

        [Parameter(Mandatory = $true)]
        [string]
        $DataSetPath,
        
        [string]
        $ReportServerUri,
        
        [Alias('ReportServerCredentials')]
        [System.Management.Automation.PSCredential]
        $Credential,
        
        $Proxy
    )
    
    Begin
    {
        $Proxy = New-RsWebServiceProxyHelper -BoundParameters $PSBoundParameters
    }
    
    Process
    {
        foreach ($item in $Path)
        {
            #region Process each path passed
            if ($PSCmdlet.ShouldProcess($item, "Set datasource's DataSet $DataSetName to $DataSetPath"))
            {
                Write-Verbose "Processing: $item"
                
                try
                {
                    $dataSets = $Proxy.GetItemReferences($item, "DataSet")
                }
                catch
                {
                    throw (New-Object System.Exception("Failed to retrieve item references from Report Server for '$item': $($_.Exception.Message)", $_.Exception))
                }
                $dataSetReference = $dataSets | Where-Object { $_.Name -eq $DataSetName } | Select-Object -First 1
                
                if (-not $dataSetReference)
                {
                    throw "$item does not contain a dataSet reference with name $DataSetName"
                }
                
                $proxyNamespace = $dataSetReference.GetType().Namespace
                $dataSetReference = New-Object "$($proxyNamespace).ItemReference"
                $dataSetReference.Name = $DataSetName
                $dataSetReference.Reference = $DataSetPath
                
                Write-Verbose "Set dataSet reference '$DataSetName' of item $item to $DataSetPath"
                $Proxy.SetItemReferences($item, @($dataSetReference))
            }
            #endregion Process each path passed
        }
    }
}

New-Alias -Name Set-RsDataSet -Value Set-RsDataSetReference -Scope Global