Functions/CatalogItems/Rest/Write-RsRestFolderContent.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# Copyright (c) 2016 Microsoft Corporation. All Rights Reserved.
# Licensed under the MIT License (MIT)

function Write-RsRestFolderContent
{
    <#
        .SYNOPSIS
            This command uploads an item from disk to a report server.

        .DESCRIPTION
            This command uploads an item from disk to a report server.

        .PARAMETER Path
            Path to item to upload on disk.

        .PARAMETER RsFolder
            Folder on reportserver to upload the item to.

        .PARAMETER Overwrite
            Overwrite the old entry, if an existing catalog item with same name exists at the specified destination.

        .PARAMETER ReportPortalUri
            Specify the Report Portal URL to your SQL Server Reporting Services Instance.

        .PARAMETER RestApiVersion
            Specify the version of REST Endpoint to use. Valid values are: "v1.0", "v2.0".
            NOTE:
                - v1.0 of REST Endpoint is not supported by Microsoft and is for SSRS 2016.
                - v2.0 of REST Endpoint is supported by Microsoft and is for SSRS 2017, PBIRS October 2017 and newer releases.

        .PARAMETER Credential
            Specify the credentials to use when connecting to the Report Server.

        .PARAMETER WebSession
            Specify the session to be used when making calls to REST Endpoint.

        .EXAMPLE
            Write-RsRestFolderContent -Path 'c:\reports' -RsFolder '/'
            
            Description
            -----------
            Uploads all the items found under 'C:\reports' to folder '/' using v2.0 REST Endpoint of Report Server located at http://localhost/reports.

        .EXAMPLE
            Write-RsRestFolderContent -Path 'c:\reports' -RsFolder '/' -RestApiVersion 'v1.0'
            
            Description
            -----------
            Uploads all the items found under 'C:\reports' to folder '/' using v1.0 REST Endpoint of Report Server located at http://localhost/reports.

        .EXAMPLE
            Write-RsRestFolderContent -WebSession $mySession -Path 'c:\reports' -RsFolder '/'
            
            Description
            -----------
            Uploads all the items found under 'C:\reports' to folder '/' using v2.0 REST Endpoint of the Report Server identified by the WebSession object.

        .EXAMPLE
            Write-RsRestFolderContent -ReportPortalUri 'http://myserver/reports' -Path 'c:\reports' -RsFolder '/'
            
            Description
            -----------
            Uploads all the items found under 'C:\reports' to folder '/' using v2.0 REST Endpoint of the Report Server located at http://myserver/reports.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $True, ValueFromPipeline = $true)]
        [string[]]
        $Path,

        [switch]
        $Recurse,

        [Parameter(Mandatory = $True)]
        [string]
        $RsFolder,

        [Alias('Override')]
        [switch]
        $Overwrite,

        [string]
        $ReportPortalUri,

        [Alias('ApiVersion')]
        [ValidateSet("v1.0", "v2.0")]
        [string]
        $RestApiVersion = "v2.0",

        [Alias('ReportServerCredentials')]
        [System.Management.Automation.PSCredential]
        $Credential,

        [Microsoft.PowerShell.Commands.WebRequestSession]
        $WebSession
    )
    Begin
    {
        $WebSession = New-RsRestSessionHelper -BoundParameters $PSBoundParameters
        $ReportPortalUri = Get-RsPortalUriHelper -WebSession $WebSession
        $catalogItemsUri = $ReportPortalUri + "api/$RestApiVersion/CatalogItems"
        $folderUri = $ReportPortalUri + "api/$RestApiVersion/Folders(Path='{0}')"
    }
    Process
    {
        if (!(Test-Path -Path $Path -PathType Container))
        {
            throw "No folder found at $Path!"
        }
        $sourceFolder = Get-Item $Path

        if ($Recurse)
        {
            $items = Get-ChildItem -Path $Path -Recurse
        }
        else
        {
            $items = Get-ChildItem -Path $Path
        }

        foreach ($item in $items)
        {
            if (($item.PSIsContainer) -and $Recurse)
            {
                $relativePath = Clear-Substring -string $item.FullName -substring $sourceFolder.FullName.TrimEnd("\") -position front
                $relativePath = Clear-Substring -string $relativePath -substring ("\" + $item.Name) -position back
                $relativePath = $relativePath.replace("\", "/")

                $folderUriPath = $null
                $folderExists = $null
                $folderInfo = $null
                if ($RsFolder -eq "/" -and $relativePath -ne "")
                {
                    $parentFolder = $relativePath
                    $folderUriPath = "$RsFolder/$($item.name)"
                }
                else
                {
                    $parentFolder = $RsFolder + $relativePath
                    if ($RsFolder -eq "/")
                    {
                        $folderUriPath = $RsFolder + $($item.name)
                    }
                    else
                    {
                        $folderUriPath = "$RsFolder/$($item.name)"
                    }
                }

                $uri = [String]::Format($folderUri, $folderUriPath)

                try
                {
                    # Try to get folder info
                    if ($Credential -ne $null)
                    {
                        $response = Invoke-WebRequest -Uri $uri -Method Get -WebSession $WebSession -Credential $Credential -Verbose:$false
                    }
                    else
                    {
                        $response = Invoke-WebRequest -Uri $uri -Method Get -WebSession $WebSession -UseDefaultCredentials -Verbose:$false
                    }

                    # parsing response to get folder name
                    $folderInfo = ConvertFrom-Json $response.Content
                    if ($folderInfo.Name -eq $item.Name)
                    {
                        $folderExists = $true
                    }
                }
                catch
                {
                    # Folder not found (404)
                    if ($_.Exception.Response -ne $null -and $_.Exception.Response.StatusCode -eq 404)
                    {
                        $folderExists = $false
                    }
                }

                if ($folderExists)
                {
                    Write-Verbose "Folder $($item.Name) already exits. Skipping."
                }
                else
                {
                    New-RsRestFolder -WebSession $WebSession -RestApiVersion $RestApiVersion -FolderName $item.Name -RsFolder $parentFolder | Out-Null
                }
            }

            if ($item.Extension -ne "")
            {
                $relativePath = Clear-Substring -string $item.FullName -substring $sourceFolder.FullName.TrimEnd("\") -position front
                $relativePath = Clear-Substring -string $relativePath -substring ("\" + $item.Name) -position back
                $relativePath = $relativePath.replace("\", "/")

                if ($RsFolder -eq "/" -and $relativePath -ne "")
                {
                    $parentFolder = $relativePath
                }
                else
                {
                    $parentFolder = $RsFolder + $relativePath
                }

                try
                {
                    Write-RsRestCatalogItem -WebSession $WebSession -RestApiVersion $RestApiVersion -Path $item.FullName -RsFolder $parentFolder -Overwrite:$Overwrite -Credential $Credential
                }
                catch
                {
                    Write-Error "Failed to create catalog item from '$($item.FullName)' in '$parentFolder': If the catalog item already exists (error: (409) Conflict), you can specify the -Overwrite parameter. $($_.Exception)"
                }
            }
        }
    }

}