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


function Set-RsDataSourceReference
{
    <#
        .SYNOPSIS
            Overrides the reference of a report or dataset to a shared data source.
        
        .DESCRIPTION
            Overrides the reference of a report or dataset to a shared data source.
        
        .PARAMETER Path
            Path of the report or dataset.
        
        .PARAMETER DataSourceName
            Name of the datasource reference to override.
        
        .PARAMETER DataSourcePath
            Path to the shared data source 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-RsDataSourceReference -Path /DataSet -DataSourceName DataSource1 -DataSourcePath /Datasources/SampleSource
            
            Description
            -----------
            Sets the dataset reference 'DataSource1' of dataset '/DataSet' to point to datasource '/Datasources/SampleSource'
    #>

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

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

        [Parameter(Mandatory = $true)]
        [string]
        $DataSourcePath,
        
        [string]
        $ReportServerUri,
        
        [Alias('ReportServerCredentials')]
        [System.Management.Automation.PSCredential]
        $Credential,
        
        $Proxy
    )
    
    Begin
    {
        $Proxy = New-RsWebServiceProxyHelper -BoundParameters $PSBoundParameters
    }
    
    Process
    {
        foreach ($item in $Path)
        {
            # For when running on Windows 6.1 with a permissive erroraction setting
            $dataSets = $null
            $dataSourceReference = $null
            
            try
            {
                $dataSets = $Proxy.GetItemReferences($item, "DataSource")
            }
            catch
            {
                throw (New-Object System.Exception("Failed to retrieve datasource item references for '$item': $($_.Exception.Message)", $_.Exception))
            }
            $dataSourceReference = $dataSets | Where-Object { $_.Name -eq $DataSourceName } | Select-Object -First 1
            
            if (-not $dataSourceReference)
            {
                throw "$item does not contain a dataSource reference with name $DataSourceName"
            }
            
            $proxyNamespace = $dataSourceReference.GetType().Namespace
            $dataSourceReference = New-Object "$($proxyNamespace).ItemReference"
            $dataSourceReference.Name = $DataSourceName
            $dataSourceReference.Reference = $DataSourcePath
            
            Write-Verbose "Set dataSource reference '$DataSourceName' of item $item to $DataSourcePath"
            try
            {
                $Proxy.SetItemReferences($item, @($dataSourceReference))
            }
            catch
            {
                throw (New-Object System.Exception("Failed to update datasource item references for '$item': $($_.Exception.Message)", $_.Exception))
            }
        }
    }
}

New-Alias -Name "Set-RsSharedDataSource" -Value Set-RsDataSourceReference -Scope Global