internal/functions/Get-DbaReportingService.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
function Get-DbaReportingService {
    <#
    .SYNOPSIS
        Gets the SQL Server Reporting Services on a computer.
 
    .DESCRIPTION
        Gets the SQL Server Reporting Services on one or more computers.
 
        Requires Local Admin rights on destination computer(s).
 
    .PARAMETER ComputerName
        The target SQL Server instance or instances.
 
    .PARAMETER InstanceName
        Only returns services that belong to the specific instances.
 
    .PARAMETER Credential
        Credential object used to connect to the computer as a different user.
 
    .PARAMETER ServiceName
        Can be used to specify service names explicitly, without looking for service types/instances.
 
    .PARAMETER EnableException
        By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
        This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
        Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
 
    .NOTES
        Tags: Service, SqlServer, Instance, Connect
        Author: Kirill Kravtsov ( @nvarscar )
 
        Website: https://dbatools.io
        Copyright: (c) 2018 by dbatools, licensed under MIT
        License: MIT https://opensource.org/licenses/MIT
 
    .LINK
        https://dbatools.io/Get-DbaService
 
    .EXAMPLE
        PS C:\> Get-DbaReportingService -ComputerName sqlserver2014a
 
        Gets the Reporting Services on computer sqlserver2014a.
 
    .EXAMPLE
        PS C:\> $cred = Get-Credential WindowsUser
        PS C:\> Get-DbaReportingService -ComputerName sql1,sql2 -Credential $cred | Out-GridView
 
        Gets the Reporting Services on computers sql1 and sql2 via the user WindowsUser, and shows them in a grid view.
 
    .EXAMPLE
        PS C:\> Get-DbaReportingService -ComputerName sql1,sql2 -InstanceName MSSQLSERVER
 
        Gets the Reporting Services related to the default instance MSSQLSERVER on computers sql1 and sql2.
 
    .EXAMPLE
        PS C:\> $MyServers = Get-Content .\servers.txt
        PS C:\> Get-DbaReportingService -ComputerName $MyServers -ServiceName MSSQLSERVER,SQLSERVERAGENT
 
        Gets the Reporting Services with ServiceName MSSQLSERVER or SQLSERVERAGENT for all the servers that are stored in the file. Every line in the file can only contain one hostname for a server.
 
    .EXAMPLE
        PS C:\> $services = Get-DbaReportingService -ComputerName sql1
        PS C:\> $services.ChangeStartMode('Manual')
 
        Gets the Reporting Services on computer sql1 and changes their startup mode to 'Manual'.
 
    .EXAMPLE
        PS C:\> (Get-DbaReportingService -ComputerName sql1).Restart($true)
 
        Calls a Restart method for each Reporting service on computer sql1.
 
    #>

    [CmdletBinding()]
    param (
        [parameter(ValueFromPipeline, Position = 1)]
        [Alias("cn", "host", "Server")]
        [DbaInstanceParameter[]]$ComputerName = $env:COMPUTERNAME,
        [Alias("Instance")]
        [string[]]$InstanceName,
        [PSCredential]$Credential,
        [string[]]$ServiceName,
        [switch]$EnableException
    )
    begin {
        $searchClause = ""
        #If filtering by service name create a dynamic WHERE clause
        if ($ServiceName) {
            $searchClause = " WHERE ServiceName = '$($ServiceName -join "' OR ServiceName = '")'"
        }
    }
    process {
        foreach ($computer in $ComputerName) {
            Write-Message -Level VeryVerbose -Message "Getting Reporting Server namespace on $Computer" -Target $Computer
            try {
                $namespaces = Get-DbaCmObject -ComputerName $Computer -NameSpace root\Microsoft\SQLServer\ReportServer -Query "Select Name FROM __NAMESPACE" -EnableException -Credential $credential
            } catch {
                Write-Message -Level Verbose -Message "No SQLServer\ReportServer Namespace on $Computer. Please note that this function is available from SQL 2005 up."
                return
            }
            # querying SqlService namespace
            ForEach ($namespace in $namespaces) {
                Write-Message -Level Verbose -Message "Getting version from the namespace $($namespace.Name) on $Computer." -Target $Computer
                try {
                    $namespaceVersion = Get-DbaCmObject -ComputerName $Computer -Namespace "root\Microsoft\SQLServer\ReportServer\$($namespace.Name)" -Query "SELECT Name FROM __NAMESPACE" -EnableException
                } catch {
                    Stop-Function -EnableException $EnableException -Message "No version Namespace on $Computer. Please note that this function is available from SQL 2005 up." -Continue
                }
                try {
                    $cimQuery = "SELECT * FROM MSReportServer_ConfigurationSetting" + $searchClause
                    $services = Get-DbaCmObject -ComputerName $Computer -Namespace "root\Microsoft\SQLServer\ReportServer\$($namespace.Name)\$($namespaceVersion.Name)\Admin" -Query $cimQuery -EnableException
                } catch {
                    Stop-Function -EnableException $EnableException -Message "Failed to acquire services from namespace $($namespace.Name)\$($namespaceVersion.Name)." -Target $Computer -ErrorRecord $_ -Continue
                }
                ForEach ($service in $services) {
                    if (!$InstanceName -or $service.InstanceName -in $InstanceName) {
                        #Add other properties and methods
                        Add-Member -Force -InputObject $service -MemberType NoteProperty -Name ServiceType -Value 'SSRS'
                        Add-Member -Force -InputObject $service -MemberType AliasProperty -Name StartName -Value WindowsServiceIdentityActual

                        try {
                            $service32 = Get-DbaCmObject -ComputerName $Computer -Namespace "root\cimv2" -Query "SELECT * FROM Win32_Service WHERE Name = '$($service.ServiceName)'" -EnableException
                        } catch {
                            Stop-Function -EnableException $EnableException -Message "Failed to acquire services32" -Target $Computer -ErrorRecord $_ -Continue
                        }
                        Add-Member -Force -InputObject $service -MemberType NoteProperty -Name ComputerName -Value $service32.SystemName
                        Add-Member -Force -InputObject $service -MemberType NoteProperty -Name State -Value $service32.State
                        $startMode = switch ($service32.StartMode) {
                            Auto { 'Automatic' } #Replacing for consistency to match other SQL Services
                            default { $_ }
                        }
                        Add-Member -Force -InputObject $service -MemberType NoteProperty -Name StartMode -Value $startMode
                        Add-Member -Force -InputObject $service -MemberType NoteProperty -Name DisplayName -Value $service32.DisplayName
                        Add-Member -Force -InputObject $service -NotePropertyName ServicePriority -NotePropertyValue 100
                        Add-Member -Force -InputObject $service -MemberType ScriptMethod -Name "Stop" -Value {
                            param ([bool]$Force = $false)
                            Stop-DbaService -InputObject $this -Force:$Force
                        }
                        Add-Member -Force -InputObject $service -MemberType ScriptMethod -Name "Start" -Value { Start-DbaService -InputObject $this }
                        Add-Member -Force -InputObject $service -MemberType ScriptMethod -Name "Restart" -Value {
                            param ([bool]$Force = $false)
                            Restart-DbaService -InputObject $this -Force:$Force
                        }
                        Add-Member -Force -InputObject $service -MemberType ScriptMethod -Name "ChangeStartMode" -Value {
                            param (
                                [parameter(Mandatory)]
                                [string]$Mode
                            )
                            $supportedModes = @("Automatic", "Manual", "Disabled")
                            if ($Mode -notin $supportedModes) {
                                Stop-Function -Message ("Incorrect mode '$Mode'. Use one of the following values: {0}" -f ($supportedModes -join ' | ')) -EnableException $false -FunctionName 'Get-DbaService'
                                Return
                            }
                            Set-ServiceStartMode -InputObject $this -Mode $Mode -ErrorAction Stop
                            $this.StartMode = $Mode
                        }
                        $defaults = "ComputerName", "ServiceName", "ServiceType", "InstanceName", "DisplayName", "StartName", "State", "StartMode"
                        Select-DefaultView -InputObject $service -Property $defaults -TypeName DbaSqlService
                    }
                }
            }
        }
    }
    end {

    }
}