-
Notifications
You must be signed in to change notification settings - Fork 225
/
MSFT_xSQLServerRSConfig.psm1
225 lines (185 loc) · 8.33 KB
/
MSFT_xSQLServerRSConfig.psm1
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
219
220
221
222
223
224
225
Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) `
-ChildPath 'xSQLServerHelper.psm1') `
-Force
<#
.SYNOPSIS
Gets the SQL Reporting Services initialization status.
.PARAMETER InstanceName
Name of the SQL Server Reporting Services instance to be configured.
.PARAMETER RSSQLServer
Name of the SQL Server to host the Reporting Service database.
.PARAMETER RSSQLInstanceName
Name of the SQL Server instance to host the Reporting Service database.
#>
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$InstanceName,
[Parameter(Mandatory = $true)]
[System.String]
$RSSQLServer,
[Parameter(Mandatory = $true)]
[System.String]
$RSSQLInstanceName
)
$instanceNamesRegistryKey = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS'
if ( Get-ItemProperty -Path $instanceNamesRegistryKey -Name $InstanceName -ErrorAction SilentlyContinue )
{
$instanceId = (Get-ItemProperty -Path $instanceNamesRegistryKey -Name $InstanceName).$InstanceName
$sqlVersion = ((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instanceId\Setup" -Name 'Version').Version).Split('.')[0]
$reportingServicesConfiguration = Get-WmiObject -Class MSReportServer_ConfigurationSetting -Namespace "root\Microsoft\SQLServer\ReportServer\RS_$InstanceName\v$sqlVersion\Admin"
if ( $reportingServicesConfiguration.DatabaseServerName.Contains('\') )
{
$RSSQLServer = $reportingServicesConfiguration.DatabaseServerName.Split('\')[0]
$RSSQLInstanceName = $reportingServicesConfiguration.DatabaseServerName.Split('\')[1]
}
else
{
$RSSQLServer = $reportingServicesConfiguration.DatabaseServerName
$RSSQLInstanceName = 'MSSQLSERVER'
}
$isInitialized = $reportingServicesConfiguration.IsInitialized
}
else
{
throw New-TerminatingError -ErrorType SSRSNotFound -FormatArgs @($InstanceName) -ErrorCategory ObjectNotFound
}
$returnValue = @{
InstanceName = $InstanceName
RSSQLServer = $RSSQLServer
RSSQLInstanceName = $RSSQLInstanceName
IsInitialized = $isInitialized
}
$returnValue
}
<#
.SYNOPSIS
Initializes SQL Reporting Services.
.PARAMETER InstanceName
Name of the SQL Server Reporting Services instance to be configured.
.PARAMETER RSSQLServer
Name of the SQL Server to host the Reporting Service database.
.PARAMETER RSSQLInstanceName
Name of the SQL Server instance to host the Reporting Service database.
#>
function Set-TargetResource
{
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$InstanceName,
[Parameter(Mandatory = $true)]
[System.String]
$RSSQLServer,
[Parameter(Mandatory = $true)]
[System.String]
$RSSQLInstanceName
)
$instanceNamesRegistryKey = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS'
if ( Get-ItemProperty -Path $instanceNamesRegistryKey -Name $InstanceName -ErrorAction SilentlyContinue )
{
<#
Import-SQLPSModule cmdlet will import SQLPS (SQL 2012/14) or SqlServer module (SQL 2016),
and if importing SQLPS, change directory back to the original one, since SQLPS changes the
current directory to SQLSERVER:\ on import.
#>
Import-SQLPSModule
$instanceId = (Get-ItemProperty -Path $instanceNamesRegistryKey -Name $InstanceName).$InstanceName
$sqlVersion = [System.Int32]((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instanceId\Setup" -Name 'Version').Version).Split('.')[0]
if ( $InstanceName -eq 'MSSQLSERVER' )
{
$reportingServicesServiceName = 'ReportServer'
$reportServerVirtualDirectoryName = 'ReportServer'
$reportsVirtualDirectoryName = 'Reports'
$reportingServicesDatabaseName = 'ReportServer'
}
else
{
$reportingServicesServiceName = "ReportServer`$$InstanceName"
$reportServerVirtualDirectoryName = "ReportServer_$InstanceName"
$reportsVirtualDirectoryName = "Reports_$InstanceName"
$reportingServicesDatabaseName = "ReportServer`$$InstanceName"
}
if ( $RSSQLInstanceName -eq 'MSSQLSERVER' )
{
$reportingServicesConnnection = $RSSQLServer
}
else
{
$reportingServicesConnnection = "$RSSQLServer\$RSSQLInstanceName"
}
$language = (Get-WMIObject -Class Win32_OperatingSystem -Namespace root/cimv2 -ErrorAction SilentlyContinue).OSLanguage
$reportingServicesConfiguration = Get-WmiObject -Class MSReportServer_ConfigurationSetting -Namespace "root\Microsoft\SQLServer\ReportServer\RS_$InstanceName\v$sqlVersion\Admin"
if ( $reportingServicesConfiguration.VirtualDirectoryReportServer -ne $reportServerVirtualDirectoryName )
{
$null = $reportingServicesConfiguration.SetVirtualDirectory('ReportServerWebService', $reportServerVirtualDirectoryName, $language)
$null = $reportingServicesConfiguration.ReserveURL('ReportServerWebService', 'http://+:80', $language)
}
if ( $reportingServicesConfiguration.VirtualDirectoryReportManager -ne $reportsVirtualDirectoryName )
{
<#
SSRS Web Portal application name changed in SQL Server 2016
https://docs.microsoft.com/en-us/sql/reporting-services/breaking-changes-in-sql-server-reporting-services-in-sql-server-2016
#>
if ($sqlVersion -ge 13)
{
$virtualDirectoryName = 'ReportServerWebApp'
}
else
{
$virtualDirectoryName = 'ReportManager'
}
$null = $reportingServicesConfiguration.SetVirtualDirectory($virtualDirectoryName, $reportsVirtualDirectoryName, $language)
$null = $reportingServicesConfiguration.ReserveURL($virtualDirectoryName, 'http://+:80', $language)
}
$reportingServicesDatabaseScript = $reportingServicesConfiguration.GenerateDatabaseCreationScript($reportingServicesDatabaseName, $language, $false)
# Determine RS service account
$reportingServicesServiceAccountUserName = (Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq $reportingServicesServiceName}).StartName
$reportingServicesDatabaseRightsScript = $reportingServicesConfiguration.GenerateDatabaseRightsScript($reportingServicesServiceAccountUserName, $reportingServicesDatabaseName, $false, $true)
Invoke-Sqlcmd -ServerInstance $reportingServicesConnnection -Query $reportingServicesDatabaseScript.Script
Invoke-Sqlcmd -ServerInstance $reportingServicesConnnection -Query $reportingServicesDatabaseRightsScript.Script
$null = $reportingServicesConfiguration.SetDatabaseConnection($reportingServicesConnnection, $reportingServicesDatabaseName, 2, '', '')
$null = $reportingServicesConfiguration.InitializeReportServer($reportingServicesConfiguration.InstallationID)
}
if ( !(Test-TargetResource @PSBoundParameters) )
{
throw New-TerminatingError -ErrorType TestFailedAfterSet -ErrorCategory InvalidResult
}
}
<#
.SYNOPSIS
Tests the SQL Reporting Services initialization status.
.PARAMETER InstanceName
Name of the SQL Server Reporting Services instance to be configured.
.PARAMETER RSSQLServer
Name of the SQL Server to host the Reporting Service database.
.PARAMETER RSSQLInstanceName
Name of the SQL Server instance to host the Reporting Service database.
#>
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[System.String]
$InstanceName,
[Parameter(Mandatory = $true)]
[System.String]
$RSSQLServer,
[Parameter(Mandatory = $true)]
[System.String]
$RSSQLInstanceName
)
$result = (Get-TargetResource @PSBoundParameters).IsInitialized
$result
}
Export-ModuleMember -Function *-TargetResource