Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code to handle restarting RS service with alternate credential #229

Merged
merged 1 commit into from
May 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 59 additions & 30 deletions ReportingServicesTools/Functions/Admin/Restore-RsEncryptionKey.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,38 @@ function Restore-RSEncryptionKey
<#
.SYNOPSIS
This script restores the SQL Server Reporting Services encryption key.

.DESCRIPTION
This script restores encryption key for SQL Server Reporting Services. This key is needed in order to read all the encrypted content stored in the Reporting Services Catalog database.

.PARAMETER Password
Specify the password that was used when the encryption key was backed up.

.PARAMETER KeyPath
Specify the path to where the encryption key is stored.

.PARAMETER ReportServerInstance
Specify the name of the SQL Server Reporting Services Instance.
Use the "Connect-RsReportServer" function to set/update a default value.

.PARAMETER ReportServerVersion
Specify the version of the SQL Server Reporting Services Instance.
Use the "Connect-RsReportServer" function to set/update a default value.

.PARAMETER ComputerName
The Report Server to target.
Use the "Connect-RsReportServer" function to set/update a default value.

.PARAMETER Credential
Specify the credentials to use when connecting to the Report Server.
Use the "Connect-RsReportServer" function to set/update a default value.

.EXAMPLE
Restore-RSEncryptionKey -Password 'Enter Your Password' -KeyPath 'C:\ReportingServices\Default.snk'
Description
-----------
This command will restore the encryption key to the default instance from SQL Server 2016 Reporting Services

.EXAMPLE
Restore-RSEncryptionKey -ReportServerInstance 'SQL2012' -ReportServerVersion '11' -Password 'Enter Your Password' -KeyPath 'C:\ReportingServices\Default.snk'
Description
Expand All @@ -51,34 +51,34 @@ function Restore-RSEncryptionKey
[Parameter(Mandatory = $True)]
[string]
$Password,

[Parameter(Mandatory = $True)]
[string]
$KeyPath,

[Alias('SqlServerInstance')]
[string]
$ReportServerInstance,

[Alias('SqlServerVersion')]
[Microsoft.ReportingServicesTools.SqlServerVersion]
$ReportServerVersion,

[string]
$ComputerName,

[System.Management.Automation.PSCredential]
$Credential
)

if ($PSCmdlet.ShouldProcess((Get-ShouldProcessTargetWmi -BoundParameters $PSBoundParameters), "Restore encryptionkey from file $KeyPath"))
{
$rsWmiObject = New-RsConfigurationSettingObjectHelper -BoundParameters $PSBoundParameters

$KeyPath = Resolve-Path $KeyPath

$reportServerService = 'ReportServer'

if ($rsWmiObject.InstanceName -ne "MSSQLSERVER")
{
if($rsWmiObject.InstanceName -eq "PBIRS")
Expand All @@ -90,13 +90,13 @@ function Restore-RSEncryptionKey
$reportServerService = $reportServerService + '$' + $rsWmiObject.InstanceName
}
}

Write-Verbose "Checking if key file path is valid..."
if (-not (Test-Path $KeyPath))
{
throw "No key was found at the specified location: $path"
}

try
{
$keyBytes = [System.IO.File]::ReadAllBytes($KeyPath)
Expand All @@ -105,10 +105,10 @@ function Restore-RSEncryptionKey
{
throw
}

Write-Verbose "Restoring encryption key..."
$restoreKeyResult = $rsWmiObject.RestoreEncryptionKey($keyBytes, $keyBytes.Length, $Password)

if ($restoreKeyResult.HRESULT -eq 0)
{
Write-Verbose "Success!"
Expand All @@ -117,17 +117,46 @@ function Restore-RSEncryptionKey
{
throw "Failed to restore the encryption key! Errors: $($restoreKeyResult.ExtendedErrors)"
}

try
{
$service = Get-Service -Name $reportServerService -ComputerName $rsWmiObject.PSComputerName -ErrorAction Stop
Write-Verbose "Stopping Reporting Services Service... $reportServerService"
$service.Stop()
$service.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Stopped)

Write-Verbose "Starting Reporting Services Service... $reportServerService"
$service.Start()
$service.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running)
# Restarting the Reporting Services Windows service requires a different method if a credential is passed because
# Get-Service does not have the ability to connect to a remote system with an alternate credential.
if ($PSBoundParameters.ContainsKey('Credential'))
{
$getServiceParams = @{
Class = 'Win32_Service'
Filter = "Name = '$reportServerService'"
ComputerName = $rsWmiObject.PSComputerName
Credential = $Credential
}
$service = Get-WmiObject @getServiceParams

Write-Verbose "Stopping Reporting Services Service... $reportServerService"
$null = $service.StopService()
do {
$service = Get-WmiObject @getServiceParams
Start-Sleep -Seconds 1
} until ($service.State -eq 'Stopped')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do a timeout or number of retries , otherwise it had a change to get stuck here in a infinite loop if the service don't stop

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I considered that. However, in the current implementation WaitForStatus will wait indefinitely, so I was just mimicking the same behavior.

$service.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Stopped)

What do you think is a reasonable amount of time/retries?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would think a couple of minutes (120 retries) should be good enough


In reply to: 282992334 [](ancestors = 282992334)


Write-Verbose "Starting Reporting Services Service... $reportServerService"
$null = $service.StartService()
do {
$service = Get-WmiObject @getServiceParams
Start-Sleep -Seconds 1
} until ($service.State -eq 'Running')
}
else
{
$service = Get-Service -Name $reportServerService -ComputerName $rsWmiObject.PSComputerName -ErrorAction Stop
Write-Verbose "Stopping Reporting Services Service... $reportServerService"
$service.Stop()
$service.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Stopped)

Write-Verbose "Starting Reporting Services Service... $reportServerService"
$service.Start()
$service.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running)
}
}
catch
{
Expand Down