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

function New-RsWebServiceProxy
{
    <#
        .SYNOPSIS
            This script creates a web service proxy object to the Reporting Services SOAP endpoint.
        
        .DESCRIPTION
            This script creates a web service proxy object to the Reporting Services SOAP endpoint associated to the Report Server URI specified by the user.
    
            By default, this function uses the connection values set by Connect-RsReportingServer.
            The defaults without ever calling Connect-RsReportingServer are:
            - ReportServerUri: http://localhost/reportserver/
            - Credential: Current User
        
        .PARAMETER ReportServerUri
            Specify the Report Server URL to your SQL Server Reporting Services Instance.
            By Default, this parameter uses the uri specified by the Connect-RsReportingServer function.
        
        .PARAMETER Credential
            Specify the Credential to use when connecting to your SQL Server Reporting Services Instance.
            By Default, this parameter uses the credentials (if any) specified by the Connect-RsReportingServer function.
        
        .PARAMETER ApiVersion
            The version of the API to use, 2010 by default. Sepcifiy '2005' or '2006' if you need
            to query a Sql Server Reporting Service Instance running a version prior to
            SQL Server 2008 R2 to access those respective APIs.
                    
        .EXAMPLE
            New-RsWebServiceProxy
            Description
            -----------
            This command will create and return a web service proxy to the default server using default credentials.
        
        .EXAMPLE
            New-RsWebServiceProxy -ReportServerUri http://myserver/reportserver_sql2012
            Description
            -----------
            This command will create and return a web service proxy to the Report Server located at http://myserver/reportserver_sql2012 using current user's credentials.
        
        .EXAMPLE
            New-RsWebServiceProxy -Credential 'CaptainAwesome'
            Description
            -----------
            This command will create and return a web service proxy to the Report Server located at http://localhost/reportserver using CaptainAwesome's credentials.
    #>


    [cmdletbinding()]
    param
    (
        [string]
        $ReportServerUri = ([Microsoft.ReportingServicesTools.ConnectionHost]::ReportServerUri),
        
        [Alias('Credentials')]
        [AllowNull()]
        [System.Management.Automation.PSCredential]
        $Credential = ([Microsoft.ReportingServicesTools.ConnectionHost]::Credential),

        [ValidateSet('2005','2006','2010')]
        [string]
        $ApiVersion = '2010'
    )
    
    #region If we did not specify a connection parameter, use a default connection
    if (-not ($PSBoundParameters.ContainsKey("ReportServerUri") -or $PSBoundParameters.ContainsKey("Credential")))
    {
        if ([Microsoft.ReportingServicesTools.ConnectionHost]::Proxy)
        {
            return ([Microsoft.ReportingServicesTools.ConnectionHost]::Proxy)
        }
        else
        {
            try
            {
                $proxy = New-RsWebServiceProxy -ReportServerUri ([Microsoft.ReportingServicesTools.ConnectionHost]::ReportServerUri) -Credential ([Microsoft.ReportingServicesTools.ConnectionHost]::Credential) -ErrorAction Stop
                [Microsoft.ReportingServicesTools.ConnectionHost]::Proxy = $proxy
                return $proxy
            }
            catch
            {
                throw (New-Object System.Exception("Failed to establish proxy connection to $([Microsoft.ReportingServicesTools.ConnectionHost]::ReportServerUri) : $($_.Exception.Message)", $_.Exception))
            }
        }
    }
    #endregion If we did not specify a connection parameter, use a default connection

    #region Build explicitly required proxy object
    # forming the full URL to the SOAP Proxy of ReportServerUri
    if ($ReportServerUri -notlike '*/') 
    {
        $ReportServerUri = $ReportServerUri + '/'
    }
    $reportServerUriObject = New-Object System.Uri($ReportServerUri)
    $soapEndpointUriObject = New-Object System.Uri($reportServerUriObject, "ReportService$ApiVersion.asmx")
    $ReportServerUri = $soapEndPointUriObject.ToString()
    
    # creating proxy either using specified credentials or default credentials
    try
    {
        Write-Verbose "Establishing proxy connection to $ReportServerUri..."
        if ($Credential)
        {
            New-WebServiceProxy -Uri $ReportServerUri -Credential $Credential -ErrorAction Stop
        }
        else
        {
            New-WebServiceProxy -Uri $ReportServerUri -UseDefaultCredential -ErrorAction Stop
        }
    }
    catch
    {
        throw (New-Object System.Exception("Failed to establish proxy connection to $ReportServerUri : $($_.Exception.Message)", $_.Exception))
    }
    #endregion Build explicitly required proxy object
}