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

function New-RsConfigurationSettingObject
{
    <#
        .SYNOPSIS
            This script creates a new WMI Object that is connected to Reporting Services WMI Provider.
        
        .DESCRIPTION
            This script creates a new WMI Object that is connected to Reporting Services WMI Provider.
        
        .PARAMETER ReportServerInstance
            Specify the name of the SQL Server Reporting Services Instance.
            Use the "Connect-RsReportServer" function to set/update a default value.
        
        .PARAMETER ReportServerVersion
            Specify the version of the SQL Server Reporting Services Instance.
            Use the "Connect-RsReportServer" function to set/update a default value.
        
        .PARAMETER ComputerName
            The Report Server to target.
            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 MinimumReportServerVersion
            The minimum SQL server version required in order to establish a connection. If trying to connect to a lower version, this function will error out.
            This allows a function to require such minimum version, without having to complicate code by looking up defaults and considering user input.
        
        .EXAMPLE
            New-RsConfigurationSettingObject
            Description
            -----------
            This command will retrieve and return WMI Object associated to the default instance (MSSQLSERVER) of SQL Server 2016 Reporting Services.
        
        .EXAMPLE
            New-RsConfigurationSettingObject -SqlServerInstance 'SQL2016'
            Description
            -----------
            This command will retrieve and return WMI Object associated to the named instance (SQL2016) of SQL Server 2016 Reporting Services.
        
        .EXAMPLE
            New-RsConfigurationSettingObject -SqlServerVersion '11'
            Description
            -----------
            This command will retrieve and return WMI Object associated to the default instance (MSSQLSERVER) of SQL Server 2012 Reporting Services.
        
        .EXAMPLE
            New-RsConfigurationSettingObject -SqlServerInstance 'SQL2012' -SqlServerVersion 'SQLServer2012'
            Description
            -----------
            This command will retrieve and return WMI Object associated to the named instance (SQL2012) of SQL Server 2012 Reporting Services.
    #>


    [cmdletbinding()]
    param
    (
        [Alias('SqlServerInstance')]
        [string]
        $ReportServerInstance = ([Microsoft.ReportingServicesTools.ConnectionHost]::Instance),
        
        [Alias('SqlServerVersion')]
        [Microsoft.ReportingServicesTools.SqlServerVersion]
        $ReportServerVersion = ([Microsoft.ReportingServicesTools.ConnectionHost]::Version),
        
        [string]
        $ComputerName = ([Microsoft.ReportingServicesTools.ConnectionHost]::ComputerName),
        
        [System.Management.Automation.PSCredential]
        $Credential = ([Microsoft.ReportingServicesTools.ConnectionHost]::Credential),
        
        [Alias('MinimumSqlServerVersion')]
        [Microsoft.ReportingServicesTools.SqlServerVersion]
        $MinimumReportServerVersion
    )
    
    if (($MinimumReportServerVersion) -and ($MinimumReportServerVersion -gt $ReportServerVersion))
    {
        throw (New-Object System.Management.Automation.PSArgumentException("Trying to connect to $ComputerName \ $ReportServerInstance, but it is only $ReportServerVersion when at least $MinimumReportServerVersion is required!"))
    }
    
    $getWmiObjectParameters = @{
        ErrorAction = "Stop"
        Namespace = "root\Microsoft\SqlServer\ReportServer\RS_$ReportServerInstance\v$($ReportServerVersion.Value__)\Admin"
        Class = "MSReportServer_ConfigurationSetting"
    }
    
    if ($ComputerName)
    {
        $getWmiObjectParameters["ComputerName"] = $ComputerName
    }
    if ($Credential)
    {
        $getWmiObjectParameters["Credential"] = $Credential
    }
    
    $wmiObjects = Get-WmiObject @getWmiObjectParameters
    return $wmiObjects | Where-Object { $_.InstanceName -eq $ReportServerInstance }
}