From 689ef41605a62a7d71bb3450172c1c188b5a03e6 Mon Sep 17 00:00:00 2001 From: Dan Reist Date: Thu, 28 Jul 2022 08:37:55 -0400 Subject: [PATCH] Updated initialize logic --- source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 | 53 +++++++++++--------- tests/Unit/DSC_SqlRS.Tests.ps1 | 1 + 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 b/source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1 index 975a7e6820..1c6bc9eaab 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 1f69085cf3..e475405706 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 } }