From a9b7894e8a1689a9233ed1d0543b7315ebd4c2a5 Mon Sep 17 00:00:00 2001 From: Marko Bozikovic Date: Fri, 9 Jun 2017 11:46:36 +0100 Subject: [PATCH] [#569] CR comments addressed. --- CHANGELOG.md | 2 +- .../MSFT_xSQLServerRSConfig.psm1 | 155 ++++++++++++------ 2 files changed, 102 insertions(+), 55 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 493cfadd0..0df08924a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ the target node, which complicated deployments in non-domain scenarios. Using `PsDscRunAsCredential` solves this problems for us. - - Fixed virtual directory creation for SQL Server 2016. + - Fixed virtual directory creation for SQL Server 2016 (issue #569). - Added unit tests (issue #295). - Changes to xSQLServerDatabase diff --git a/DSCResources/MSFT_xSQLServerRSConfig/MSFT_xSQLServerRSConfig.psm1 b/DSCResources/MSFT_xSQLServerRSConfig/MSFT_xSQLServerRSConfig.psm1 index 54f178a84..19cde5d0a 100644 --- a/DSCResources/MSFT_xSQLServerRSConfig/MSFT_xSQLServerRSConfig.psm1 +++ b/DSCResources/MSFT_xSQLServerRSConfig/MSFT_xSQLServerRSConfig.psm1 @@ -1,48 +1,64 @@ -$currentPath = Split-Path -Parent $MyInvocation.MyCommand.Path -Write-Debug -Message "CurrentPath: $currentPath" +Import-Module -Name (Join-Path -Path (Split-Path (Split-Path $PSScriptRoot -Parent) -Parent) ` + -ChildPath 'xSQLServerHelper.psm1') ` + -Force -# Load Common Code -Import-Module $currentPath\..\..\xSQLServerHelper.psm1 -Verbose:$false -ErrorAction Stop + +<# + .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)] + [Parameter(Mandatory = $true)] [System.String] $InstanceName, - [parameter(Mandatory = $true)] + [Parameter(Mandatory = $true)] [System.String] $RSSQLServer, - [parameter(Mandatory = $true)] + [Parameter(Mandatory = $true)] [System.String] $RSSQLInstanceName ) - if(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS" -Name $InstanceName -ErrorAction SilentlyContinue) + $instanceNamesRegistryKey = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS' + + if ( Get-ItemProperty -Path $instanceNamesRegistryKey -Name $InstanceName -ErrorAction SilentlyContinue ) { - $InstanceKey = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS" -Name $InstanceName).$InstanceName - $SQLVersion = ((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$InstanceKey\Setup" -Name "Version").Version).Split(".")[0] + $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] - $RSConfig = Get-WmiObject -Class MSReportServer_ConfigurationSetting -Namespace "root\Microsoft\SQLServer\ReportServer\RS_$InstanceName\v$SQLVersion\Admin" - if($RSConfig.DatabaseServerName.Contains("\")) + $reportingServicesConfiguration = Get-WmiObject -Class MSReportServer_ConfigurationSetting -Namespace "root\Microsoft\SQLServer\ReportServer\RS_$InstanceName\v$sqlVersion\Admin" + if ( $reportingServicesConfiguration.DatabaseServerName.Contains('\') ) { - $RSSQLServer = $RSConfig.DatabaseServerName.Split("\")[0] - $RSSQLInstanceName = $RSConfig.DatabaseServerName.Split("\")[1] + $RSSQLServer = $reportingServicesConfiguration.DatabaseServerName.Split('\')[0] + $RSSQLInstanceName = $reportingServicesConfiguration.DatabaseServerName.Split('\')[1] } else { - $RSSQLServer = $RSConfig.DatabaseServerName - $RSSQLInstanceName = "MSSQLSERVER" + $RSSQLServer = $reportingServicesConfiguration.DatabaseServerName + $RSSQLInstanceName = 'MSSQLSERVER' } - $IsInitialized = $RSConfig.IsInitialized + + $isInitialized = $reportingServicesConfiguration.IsInitialized } else - { + { throw New-TerminatingError -ErrorType SSRSNotFound -FormatArgs @($InstanceName) -ErrorCategory ObjectNotFound } @@ -50,44 +66,59 @@ function Get-TargetResource InstanceName = $InstanceName RSSQLServer = $RSSQLServer RSSQLInstanceName = $RSSQLInstanceName - IsInitialized = $IsInitialized + 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)] + [Parameter(Mandatory = $true)] [System.String] $InstanceName, - [parameter(Mandatory = $true)] + [Parameter(Mandatory = $true)] [System.String] $RSSQLServer, - [parameter(Mandatory = $true)] + [Parameter(Mandatory = $true)] [System.String] $RSSQLInstanceName ) - if(Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS" -Name $InstanceName -ErrorAction SilentlyContinue) + $instanceNamesRegistryKey = 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS' + + if ( Get-ItemProperty -Path -Name $InstanceName -ErrorAction SilentlyContinue ) { # smart import of the SQL module Import-SQLPSModule - $InstanceKey = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS" -Name $InstanceName).$InstanceName - $SQLVersion = [int]((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$InstanceKey\Setup" -Name "Version").Version).Split(".")[0] - if($InstanceName -eq "MSSQLSERVER") + $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' ) { - $RSServiceName = "ReportServer" - $RSVirtualDirectory = "ReportServer" - $RMVirtualDirectory = "Reports" - $RSDatabase = "ReportServer" + $RSServiceName = 'ReportServer' + $RSVirtualDirectory = 'ReportServer' + $RMVirtualDirectory = 'Reports' + $RSDatabase = 'ReportServer' } else { @@ -96,71 +127,87 @@ function Set-TargetResource $RMVirtualDirectory = "Reports_$InstanceName" $RSDatabase = "ReportServer`$$InstanceName" } - if($RSSQLInstanceName -eq "MSSQLSERVER") + + if ( $RSSQLInstanceName -eq 'MSSQLSERVER' ) { - $RSConnection = "$RSSQLServer" + $reportingServicesConnnection = "$RSSQLServer" } else { - $RSConnection = "$RSSQLServer\$RSSQLInstanceName" + $reportingServicesConnnection = "$RSSQLServer\$RSSQLInstanceName" } - $Language = (Get-WMIObject -Class Win32_OperatingSystem -Namespace root/cimv2 -ErrorAction SilentlyContinue).OSLanguage - $RSConfig = Get-WmiObject -Class MSReportServer_ConfigurationSetting -Namespace "root\Microsoft\SQLServer\ReportServer\RS_$InstanceName\v$SQLVersion\Admin" - if($RSConfig.VirtualDirectoryReportServer -ne $RSVirtualDirectory) + + $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 $RSVirtualDirectory ) { - $null = $RSConfig.SetVirtualDirectory("ReportServerWebService",$RSVirtualDirectory,$Language) - $null = $RSConfig.ReserveURL("ReportServerWebService","http://+:80",$Language) + $null = $reportingServicesConfiguration.SetVirtualDirectory('ReportServerWebService',$RSVirtualDirectory,$language) + $null = $reportingServicesConfiguration.ReserveURL('ReportServerWebService','http://+:80',$language) } - if($RSConfig.VirtualDirectoryReportManager -ne $RMVirtualDirectory) + + if ( $reportingServicesConfiguration.VirtualDirectoryReportManager -ne $RMVirtualDirectory ) { # 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 - $virtualDirectoryName = if ($SQLVersion -ge 13) { 'ReportServerWebApp' } else { 'ReportManager'} - $null = $RSConfig.SetVirtualDirectory($virtualDirectoryName,$RMVirtualDirectory,$Language) - $null = $RSConfig.ReserveURL($virtualDirectoryName,"http://+:80",$Language) + $virtualDirectoryName = if ($sqlVersion -ge 13) { 'ReportServerWebApp' } else { 'ReportManager'} + $null = $reportingServicesConfiguration.SetVirtualDirectory($virtualDirectoryName,$RMVirtualDirectory,$language) + $null = $reportingServicesConfiguration.ReserveURL($virtualDirectoryName,'http://+:80',$language) } - $RSCreateScript = $RSConfig.GenerateDatabaseCreationScript($RSDatabase,$Language,$false) + + $RSCreateScript = $reportingServicesConfiguration.GenerateDatabaseCreationScript($RSDatabase,$language,$false) # Determine RS service account $RSSvcAccountUsername = (Get-WmiObject -Class Win32_Service | Where-Object {$_.Name -eq $RSServiceName}).StartName - $RSRightsScript = $RSConfig.GenerateDatabaseRightsScript($RSSvcAccountUsername,$RSDatabase,$false,$true) + $RSRightsScript = $reportingServicesConfiguration.GenerateDatabaseRightsScript($RSSvcAccountUsername,$RSDatabase,$false,$true) - Invoke-Sqlcmd -ServerInstance $RSConnection -Query $RSCreateScript.Script - Invoke-Sqlcmd -ServerInstance $RSConnection -Query $RSRightsScript.Script - $null = $RSConfig.SetDatabaseConnection($RSConnection,$RSDatabase,2,"","") - $null = $RSConfig.InitializeReportServer($RSConfig.InstallationID) + Invoke-Sqlcmd -ServerInstance $reportingServicesConnnection -Query $RSCreateScript.Script + Invoke-Sqlcmd -ServerInstance $reportingServicesConnnection -Query $RSRightsScript.Script + $null = $reportingServicesConfiguration.SetDatabaseConnection($reportingServicesConnnection,$RSDatabase,2,'','') + $null = $reportingServicesConfiguration.InitializeReportServer($reportingServicesConfiguration.InstallationID) } - if(!(Test-TargetResource @PSBoundParameters)) + 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)] + [Parameter(Mandatory = $true)] [System.String] $InstanceName, - [parameter(Mandatory = $true)] + [Parameter(Mandatory = $true)] [System.String] $RSSQLServer, - [parameter(Mandatory = $true)] + [Parameter(Mandatory = $true)] [System.String] $RSSQLInstanceName ) $result = (Get-TargetResource @PSBoundParameters).IsInitialized - + $result } - Export-ModuleMember -Function *-TargetResource