Skip to content

Commit 29368ce

Browse files
author
Paul Shamus
committed
Added code to handle restarting RS service with alternate credential
1 parent ffe49b6 commit 29368ce

1 file changed

Lines changed: 59 additions & 30 deletions

File tree

ReportingServicesTools/Functions/Admin/Restore-RsEncryptionKey.ps1

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,38 @@ function Restore-RSEncryptionKey
66
<#
77
.SYNOPSIS
88
This script restores the SQL Server Reporting Services encryption key.
9-
9+
1010
.DESCRIPTION
1111
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.
12-
12+
1313
.PARAMETER Password
1414
Specify the password that was used when the encryption key was backed up.
15-
15+
1616
.PARAMETER KeyPath
1717
Specify the path to where the encryption key is stored.
18-
18+
1919
.PARAMETER ReportServerInstance
2020
Specify the name of the SQL Server Reporting Services Instance.
2121
Use the "Connect-RsReportServer" function to set/update a default value.
22-
22+
2323
.PARAMETER ReportServerVersion
2424
Specify the version of the SQL Server Reporting Services Instance.
2525
Use the "Connect-RsReportServer" function to set/update a default value.
26-
26+
2727
.PARAMETER ComputerName
2828
The Report Server to target.
2929
Use the "Connect-RsReportServer" function to set/update a default value.
30-
30+
3131
.PARAMETER Credential
3232
Specify the credentials to use when connecting to the Report Server.
3333
Use the "Connect-RsReportServer" function to set/update a default value.
34-
34+
3535
.EXAMPLE
3636
Restore-RSEncryptionKey -Password 'Enter Your Password' -KeyPath 'C:\ReportingServices\Default.snk'
3737
Description
3838
-----------
3939
This command will restore the encryption key to the default instance from SQL Server 2016 Reporting Services
40-
40+
4141
.EXAMPLE
4242
Restore-RSEncryptionKey -ReportServerInstance 'SQL2012' -ReportServerVersion '11' -Password 'Enter Your Password' -KeyPath 'C:\ReportingServices\Default.snk'
4343
Description
@@ -51,34 +51,34 @@ function Restore-RSEncryptionKey
5151
[Parameter(Mandatory = $True)]
5252
[string]
5353
$Password,
54-
54+
5555
[Parameter(Mandatory = $True)]
5656
[string]
5757
$KeyPath,
58-
58+
5959
[Alias('SqlServerInstance')]
6060
[string]
6161
$ReportServerInstance,
62-
62+
6363
[Alias('SqlServerVersion')]
6464
[Microsoft.ReportingServicesTools.SqlServerVersion]
6565
$ReportServerVersion,
66-
66+
6767
[string]
6868
$ComputerName,
69-
69+
7070
[System.Management.Automation.PSCredential]
7171
$Credential
7272
)
73-
73+
7474
if ($PSCmdlet.ShouldProcess((Get-ShouldProcessTargetWmi -BoundParameters $PSBoundParameters), "Restore encryptionkey from file $KeyPath"))
7575
{
7676
$rsWmiObject = New-RsConfigurationSettingObjectHelper -BoundParameters $PSBoundParameters
7777

7878
$KeyPath = Resolve-Path $KeyPath
79-
79+
8080
$reportServerService = 'ReportServer'
81-
81+
8282
if ($rsWmiObject.InstanceName -ne "MSSQLSERVER")
8383
{
8484
if($rsWmiObject.InstanceName -eq "PBIRS")
@@ -90,13 +90,13 @@ function Restore-RSEncryptionKey
9090
$reportServerService = $reportServerService + '$' + $rsWmiObject.InstanceName
9191
}
9292
}
93-
93+
9494
Write-Verbose "Checking if key file path is valid..."
9595
if (-not (Test-Path $KeyPath))
9696
{
9797
throw "No key was found at the specified location: $path"
9898
}
99-
99+
100100
try
101101
{
102102
$keyBytes = [System.IO.File]::ReadAllBytes($KeyPath)
@@ -105,10 +105,10 @@ function Restore-RSEncryptionKey
105105
{
106106
throw
107107
}
108-
108+
109109
Write-Verbose "Restoring encryption key..."
110110
$restoreKeyResult = $rsWmiObject.RestoreEncryptionKey($keyBytes, $keyBytes.Length, $Password)
111-
111+
112112
if ($restoreKeyResult.HRESULT -eq 0)
113113
{
114114
Write-Verbose "Success!"
@@ -117,17 +117,46 @@ function Restore-RSEncryptionKey
117117
{
118118
throw "Failed to restore the encryption key! Errors: $($restoreKeyResult.ExtendedErrors)"
119119
}
120-
120+
121121
try
122122
{
123-
$service = Get-Service -Name $reportServerService -ComputerName $rsWmiObject.PSComputerName -ErrorAction Stop
124-
Write-Verbose "Stopping Reporting Services Service... $reportServerService"
125-
$service.Stop()
126-
$service.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Stopped)
127-
128-
Write-Verbose "Starting Reporting Services Service... $reportServerService"
129-
$service.Start()
130-
$service.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running)
123+
# Restarting the Reporting Services Windows service requires a different method if a credential is passed because
124+
# Get-Service does not have the ability to connect to a remote system with an alternate credential.
125+
if ($PSBoundParameters.ContainsKey('Credential'))
126+
{
127+
$getServiceParams = @{
128+
Class = 'Win32_Service'
129+
Filter = "Name = '$reportServerService'"
130+
ComputerName = $rsWmiObject.PSComputerName
131+
Credential = $Credential
132+
}
133+
$service = Get-WmiObject @getServiceParams
134+
135+
Write-Verbose "Stopping Reporting Services Service... $reportServerService"
136+
$null = $service.StopService()
137+
do {
138+
$service = Get-WmiObject @getServiceParams
139+
Start-Sleep -Seconds 1
140+
} until ($service.State -eq 'Stopped')
141+
142+
Write-Verbose "Starting Reporting Services Service... $reportServerService"
143+
$null = $service.StartService()
144+
do {
145+
$service = Get-WmiObject @getServiceParams
146+
Start-Sleep -Seconds 1
147+
} until ($service.State -eq 'Running')
148+
}
149+
else
150+
{
151+
$service = Get-Service -Name $reportServerService -ComputerName $rsWmiObject.PSComputerName -ErrorAction Stop
152+
Write-Verbose "Stopping Reporting Services Service... $reportServerService"
153+
$service.Stop()
154+
$service.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Stopped)
155+
156+
Write-Verbose "Starting Reporting Services Service... $reportServerService"
157+
$service.Start()
158+
$service.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running)
159+
}
131160
}
132161
catch
133162
{

0 commit comments

Comments
 (0)