From d35a63d26735c7e6e442beca92566c3dcafd956b Mon Sep 17 00:00:00 2001 From: Johan Ljunggren Date: Fri, 22 Sep 2017 06:58:41 +0200 Subject: [PATCH] xSQLServerRSConfig: Restart Reporting Services after initialize (#837) - Changes to xSQLServerRSConfig - Now the resource will restart the Reporting Services service after initializing (issue #592). This will enable the Reports site to work. --- CHANGELOG.md | 3 + .../MSFT_xSQLServerRSConfig.psm1 | 2 + Tests/Unit/MSFT_xSQLServerRSConfig.Tests.ps1 | 3 + Tests/Unit/xSQLServerHelper.Tests.ps1 | 64 +++++++++++++++++++ en-US/xSQLServerHelper.strings.psd1 | 4 +- xSQLServerHelper.psm1 | 51 ++++++++++++++- 6 files changed, 123 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fbe72c52..d2011e00d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -101,6 +101,9 @@ Services has not been initialized ([issue #822](https://github.com/PowerShell/xSQLServer/issues/822)). - Fixed so that when two Reporting Services are installed for the same major version the resource does not throw an error ([issue #819](https://github.com/PowerShell/xSQLServer/issues/819)). + - Now the resource will restart the Reporting Services service after + initializing ([issue #592](https://github.com/PowerShell/xSQLServer/issues/592)). + This will enable the Reports site to work. ## 8.1.0.0 diff --git a/DSCResources/MSFT_xSQLServerRSConfig/MSFT_xSQLServerRSConfig.psm1 b/DSCResources/MSFT_xSQLServerRSConfig/MSFT_xSQLServerRSConfig.psm1 index a8041434c..d0566ca7d 100644 --- a/DSCResources/MSFT_xSQLServerRSConfig/MSFT_xSQLServerRSConfig.psm1 +++ b/DSCResources/MSFT_xSQLServerRSConfig/MSFT_xSQLServerRSConfig.psm1 @@ -192,6 +192,8 @@ function Set-TargetResource Invoke-Sqlcmd -ServerInstance $reportingServicesConnection -Query $reportingServicesDatabaseRightsScript.Script $null = $reportingServicesConfiguration.SetDatabaseConnection($reportingServicesConnection, $reportingServicesDatabaseName, 2, '', '') $null = $reportingServicesConfiguration.InitializeReportServer($reportingServicesConfiguration.InstallationID) + + Restart-ReportingServicesService -SQLInstanceName $InstanceName } if ( !(Test-TargetResource @PSBoundParameters) ) diff --git a/Tests/Unit/MSFT_xSQLServerRSConfig.Tests.ps1 b/Tests/Unit/MSFT_xSQLServerRSConfig.Tests.ps1 index fd7caad8c..db07c5421 100644 --- a/Tests/Unit/MSFT_xSQLServerRSConfig.Tests.ps1 +++ b/Tests/Unit/MSFT_xSQLServerRSConfig.Tests.ps1 @@ -260,6 +260,7 @@ try Mock -CommandName Import-SQLPSModule -Verifiable Mock -CommandName Invoke-Sqlcmd -Verifiable Mock -CommandName Get-ItemProperty -MockWith $mockGetItemProperty -Verifiable + Mock -CommandName Restart-ReportingServicesService -Verifiable } Context 'When the system is not in the desired state' { @@ -311,6 +312,7 @@ try Assert-MockCalled -CommandName Get-WmiObject -Exactly -Times 2 -Scope It Assert-MockCalled -CommandName Invoke-Sqlcmd -Exactly -Times 2 -Scope It + Assert-MockCalled -CommandName Restart-ReportingServicesService -Exactly -Times 1 -Scope It } Context 'When there is no Reporting Services instance after Set-TargetResource has been called' { @@ -365,6 +367,7 @@ try Assert-MockCalled -CommandName Get-WmiObject -Exactly -Times 2 -Scope It Assert-MockCalled -CommandName Invoke-Sqlcmd -Exactly -Times 2 -Scope It + Assert-MockCalled -CommandName Restart-ReportingServicesService -Exactly -Times 1 -Scope It } } } diff --git a/Tests/Unit/xSQLServerHelper.Tests.ps1 b/Tests/Unit/xSQLServerHelper.Tests.ps1 index 823a4be49..fae71cdc0 100644 --- a/Tests/Unit/xSQLServerHelper.Tests.ps1 +++ b/Tests/Unit/xSQLServerHelper.Tests.ps1 @@ -1619,4 +1619,68 @@ InModuleScope $script:moduleName { } } } + + Describe 'Testing Restart-ReportingServicesService' { + Context 'When restarting a Report Services default instance' { + BeforeAll { + $mockServiceName = 'ReportServer' + + Mock -CommandName Restart-Service -Verifiable + Mock -CommandName Start-Service -Verifiable + Mock -CommandName Get-Service -MockWith { + return @{ + Name = $mockServiceName + DependentServices = @( + @{ + Name = 'DependentService' + Status = 'Running' + DependentServices = @() + } + ) + } + } + } + + It 'Should restart the service and dependent service' { + { Restart-ReportingServicesService -SQLInstanceName 'MSSQLSERVER' } | Should Not Throw + + Assert-MockCalled -CommandName Get-Service -ParameterFilter { + $Name -eq $mockServiceName + } -Scope It -Exactly -Times 1 + Assert-MockCalled -CommandName Restart-Service -Scope It -Exactly -Times 1 + Assert-MockCalled -CommandName Start-Service -Scope It -Exactly -Times 1 + } + } + + Context 'When restarting a Report Services named instance' { + BeforeAll { + $mockServiceName = 'ReportServer$TEST' + + Mock -CommandName Restart-Service -Verifiable + Mock -CommandName Start-Service -Verifiable + Mock -CommandName Get-Service -MockWith { + return @{ + Name = $mockServiceName + DependentServices = @( + @{ + Name = 'DependentService' + Status = 'Running' + DependentServices = @() + } + ) + } + } + } + + It 'Should restart the service and dependent service' { + { Restart-ReportingServicesService -SQLInstanceName 'TEST' } | Should Not Throw + + Assert-MockCalled -CommandName Get-Service -ParameterFilter { + $Name -eq $mockServiceName + } -Scope It -Exactly -Times 1 + Assert-MockCalled -CommandName Restart-Service -Scope It -Exactly -Times 1 + Assert-MockCalled -CommandName Start-Service -Scope It -Exactly -Times 1 + } + } + } } diff --git a/en-US/xSQLServerHelper.strings.psd1 b/en-US/xSQLServerHelper.strings.psd1 index bcc1190df..2ae1a6e5a 100644 --- a/en-US/xSQLServerHelper.strings.psd1 +++ b/en-US/xSQLServerHelper.strings.psd1 @@ -31,8 +31,8 @@ ConvertFrom-StringData @' BringClusterResourcesOffline = Bringing the SQL Server resources {0} offline. BringSqlServerClusterResourcesOnline = Bringing the SQL Server resource back online. BringSqlServerAgentClusterResourcesOnline = Bringing the SQL Server Agent resource online. - GetSqlServerService = Getting SQL Server service information. - RestartSqlServerService = SQL Server service restarting. + GetServiceInformation = Getting {0} service information. + RestartService = {0} service restarting. StartingDependentService = Starting service {0} ExecuteQueryWithResultsFailed = Executing query with results failed on database '{0}'. ExecuteNonQueryFailed = Executing non-query failed on database '{0}'. diff --git a/xSQLServerHelper.psm1 b/xSQLServerHelper.psm1 index 15cf5bb99..771db880e 100644 --- a/xSQLServerHelper.psm1 +++ b/xSQLServerHelper.psm1 @@ -819,7 +819,7 @@ function Restart-SqlService } else { - Write-Verbose -Message ($script:localizedData.GetSqlServerService) -Verbose + Write-Verbose -Message ($script:localizedData.GetServiceInformation -f 'SQL Server') -Verbose $sqlService = Get-Service -DisplayName "SQL Server ($($serverObject.ServiceName))" <# @@ -829,7 +829,7 @@ function Restart-SqlService $agentService = $sqlService.DependentServices | Where-Object -FilterScript { $_.Status -eq 'Running' } # Restart the SQL Server service - Write-Verbose -Message ($script:localizedData.RestartSqlServerService) -Verbose + Write-Verbose -Message ($script:localizedData.RestartService -f 'SQL Server') -Verbose $sqlService | Restart-Service -Force # Start dependent services @@ -840,6 +840,53 @@ function Restart-SqlService } } +<# + .SYNOPSIS + Restarts a Reporting Services instance and associated services + + .PARAMETER SQLInstanceName + Name of the instance to be restarted. Default is 'MSSQLSERVER' + (the default instance). +#> +function Restart-ReportingServicesService +{ + [CmdletBinding()] + param + ( + [Parameter()] + [System.String] + $SQLInstanceName = 'MSSQLSERVER' + ) + + $ServiceName = 'ReportServer' + + if (-not ($SQLInstanceName -eq 'MSSQLSERVER')) + { + $ServiceName += '${0}' -f $SQLInstanceName + } + + Write-Verbose -Message ($script:localizedData.GetServiceInformation -f 'Reporting Services') -Verbose + $reportingServicesService = Get-Service -Name $ServiceName + + <# + Get all dependent services that are running. + There are scenarios where an automatic service is stopped and should + not be restarted automatically. + #> + $dependentService = $reportingServicesService.DependentServices | Where-Object -FilterScript { + $_.Status -eq 'Running' + } + + Write-Verbose -Message ($script:localizedData.RestartService -f 'Reporting Services') -Verbose + $reportingServicesService | Restart-Service -Force + + # Start dependent services + $dependentService | ForEach-Object { + Write-Verbose -Message ($script:localizedData.StartingDependentService -f $_.DisplayName) -Verbose + $_ | Start-Service + } +} + <# .SYNOPSIS Executes a query on the specified database.