Functions/CatalogItems/New-RsFolder.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-RsFolder
{
    <#
        .SYNOPSIS
            This script creates a new folder in the Report Server

        .DESCRIPTION
            This script creates a new folder in the Report Server

        .PARAMETER RsFolder
            Specify the location where the folder should be created

        .PARAMETER FolderName
            Specify the name of the the new folder

        .PARAMETER Description
            Specify the description to be added to the new folder

        .PARAMETER Hidden
            Mark the item as hidden on the destination server.

        .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
            New-RsFolder -RsFolder '/' -FolderName 'My new folder'
            Description
            -----------
            This command will establish a connection to the Report Server located at http://localhost/reportserver using current user's credentials and create a new folder 'My new folder' at the root of the SSRS instance.

        .EXAMPLE
            New-RsFolder -ReportServerUri 'http://remoteServer/reportserver' -RsFolder '/existingfolder' -FolderName 'My new sub-folder'
            Description
            -----------
            This command will establish a connection to the Report Server located at http://remoteServer/reportserver using current user's credentials and create a new folder 'My new sub-folder' at the folder existingfolder in the root.

        .EXAMPLE
            New-RsFolder -RsFolder '/' -FolderName 'MyDescriptiveReports' -Description 'This folder contains Descriptive Reports'
            Description
            -----------
            This command will establish a connection to the Report Server located at http://localhost/reportserver using current user's credentials and create a new folder 'MyDescriptiveReports' with a Description of 'This folder contains Descriptive Reports' at the root of the SSRS instance.

    #>


    [cmdletbinding()]
    param
    (
        [Parameter(Mandatory = $True)]
        [Alias('ItemPath','Path')]
        [string]
        $RsFolder,

        [Parameter(Mandatory = $True)]
        [Alias('Name')]
        [string]
        $FolderName,

        [string]
        $Description,

        [switch]
        $Hidden,

        [string]
        $ReportServerUri,

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

        $Proxy
    )

    $Proxy = New-RsWebServiceProxyHelper -BoundParameters $PSBoundParameters

    $namespace = $proxy.GetType().Namespace
    $propertyDataType = "$namespace.Property"
    $additionalProperties = New-Object System.Collections.Generic.List[$propertyDataType]
    if ($Description)
    {
        $descriptionProperty = New-Object $propertyDataType
        $descriptionProperty.Name = 'Description'
        $descriptionProperty.Value = $Description
        $additionalProperties.Add($descriptionProperty)
    }

    if ($Hidden)
    {
        $hiddenProperty = New-Object $propertyDataType
        $hiddenProperty.Name = 'Hidden'
        $hiddenProperty.Value = $Hidden
        $additionalProperties.Add($hiddenProperty)
    }

    try
    {
        Write-Verbose "Creating folder $($FolderName)..."
        $Proxy.CreateFolder($FolderName, $RsFolder, $additionalProperties) | Out-Null
        Write-Verbose "Folder $($FolderName) created successfully!"
    }
    catch
    {
        throw (New-Object System.Exception("Exception occurred while creating folder! $($_.Exception.Message)", $_.Exception))
    }
}