Skip to content

Commit

Permalink
Updated initialize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
randomnote1 committed Jul 28, 2022
1 parent 728049c commit c9c86c3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
53 changes: 30 additions & 23 deletions source/DSCResources/DSC_SqlRS/DSC_SqlRS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/DSC_SqlRS.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ Describe 'SqlRS\Set-TargetResource' -Tag 'Set' {
$mockInvokeRsCimMethod_InitializeReportServer = {
return @{
HRESULT = 0
ReturnValue = $true
}
}

Expand Down

0 comments on commit c9c86c3

Please sign in to comment.