diff --git a/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 b/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 index 975a7e682..1c6bc9eaa 100644 --- a/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 +++ b/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 @@ -1058,26 +1058,13 @@ function Set-TargetResource ) $restartReportingService = $true + $restoreKey = $false + $reportingServicesInitialized = $reportingServicesData.Configuration.IsInitialized - $invokeRsCimMethodInitializeReportServerParameters = @{ - CimInstance = $reportingServicesData.Configuration - MethodName = 'InitializeReportServer' - Arguments = @{ - InstallationId = $reportingServicesData.Configuration.InstallationID - } - } - - try + do { - Invoke-RsCimMethod @invokeRsCimMethodInitializeReportServerParameters - } - catch [System.Management.Automation.RuntimeException] - { - if ( $_.Exception -match 'The report server was unable to validate the integrity of encrypted data in the database' ) + if ( $restoreKey ) { - Write-Verbose -Message 'Received a runtime exception' -Verbose - - # Restore key here $invokeRsCimMethodRestoreEncryptionKeyParameters = @{ CimInstance = $reportingServicesData.Configuration MethodName = 'RestoreEncryptionKey' @@ -1089,18 +1076,38 @@ function Set-TargetResource } $restoreEncryptionKeyResult = Invoke-RsCimMethod @invokeRsCimMethodRestoreEncryptionKeyParameters + } - if ( $restoreEncryptionKeyResult.HRESULT -eq 0 ) - { - # Finally, try and initialize the server again - Invoke-RsCimMethod @invokeRsCimMethodInitializeReportServerParameters + try + { + $invokeRsCimMethodInitializeReportServerParameters = @{ + CimInstance = $reportingServicesData.Configuration + MethodName = 'InitializeReportServer' + Arguments = @{ + InstallationId = $reportingServicesData.Configuration.InstallationID + } } + + $initializeReportServerResult = Invoke-RsCimMethod @invokeRsCimMethodInitializeReportServerParameters + $reportingServicesInitialized = $initializeReportServerResult.ReturnValue } - else + catch [System.Management.Automation.RuntimeException] { - throw $_ + if ( $_.Exception -match 'The report server was unable to validate the integrity of encrypted data in the database' ) + { + # Restore the encryption key before trying again + $restoreKey = $true + } + else + { + throw $_ + } } } + while ( -not $reportingServicesInitialized ) + + # Refresh the reportingServicesData + $reportingServicesData = Get-ReportingServicesData -InstanceName $InstanceName } else { diff --git a/tests/Unit/DSC_SqlRS.Tests.ps1 b/tests/Unit/DSC_SqlRS.Tests.ps1 index 1f69085cf..e47540570 100644 --- a/tests/Unit/DSC_SqlRS.Tests.ps1 +++ b/tests/Unit/DSC_SqlRS.Tests.ps1 @@ -606,6 +606,7 @@ Describe 'SqlRS\Set-TargetResource' -Tag 'Set' { $mockInvokeRsCimMethod_InitializeReportServer = { return @{ HRESULT = 0 + ReturnValue = $true } }