Skip to content

Commit c6d1edc

Browse files
authored
Add files via upload
1 parent b794899 commit c6d1edc

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

SQLRS/RSCertificateBinding.ps1

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
function Set-SSLCertificateBinding {
2+
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = "Low")]
3+
[OutputType([System.Int32])]
4+
param (
5+
[Parameter(Mandatory = $false)]
6+
[ValidateNotNullOrEmpty()]
7+
[System.Security.Cryptography.X509Certificates.X509Certificate2]
8+
$Certificate = $((Get-ChildItem "cert:\LocalMachine\root").Where{ $_.Subject -eq "CN=$env:COMPUTERNAME" }),
9+
10+
[Parameter(Mandatory = $false)]
11+
[ValidateNotNullOrEmpty()]
12+
[System.String]
13+
$endpoint = $($env:endpoint)
14+
)
15+
16+
Try {
17+
18+
if ($PSCmdlet.ShouldProcess("ShouldProcess?")) {
19+
20+
$CertificateBindingParameters = @{
21+
CimInstance = $(Get-ReportingServicesData SSRS).Configuration
22+
MethodName = "CreateSSLCertificateBinding"
23+
Arguments = @{
24+
CertificateHash = [String]$Certificate.GetCertHashString().ToLower()
25+
IPAddress = [String]"0.0.0.0"
26+
Port = [Int32]443
27+
Lcid = [Int32]$(Get-Culture).LCID
28+
}
29+
}
30+
31+
$ReserveURLParameters = @{
32+
CimInstance = $(Get-ReportingServicesData SSRS).Configuration
33+
MethodName = "ReserveURL"
34+
Arguments = @{
35+
UrlString = [String]"https://$($endpoint):443"
36+
Lcid = [Int32]$(Get-Culture).LCID
37+
}
38+
}
39+
40+
foreach ( $Application in @("ReportServerWebService","ReportServerWebApp") ) {
41+
$CertificateBindingParameters.Arguments["Application"] = [String]$Application
42+
$ReserveURLParameters.Arguments["Application"] = [String]$Application
43+
$null = Invoke-RsCimMethod @CertificateBindingParameters
44+
$null = Invoke-RsCimMethod @ReserveURLParameters
45+
}
46+
47+
Restart-Service SQLServerReportingServices -Force
48+
49+
}
50+
51+
## ALL DONE
52+
Write-Verbose "ALL DONE"
53+
54+
Return 0
55+
56+
}
57+
Catch [System.Exception] {
58+
Write-Verbose "Error at line: $(($PSItem[0].InvocationInfo.line).Trim())"
59+
$PSCmdlet.ThrowTerminatingError($PSItem)
60+
}
61+
}
62+
63+
function Get-ReportingServicesData {
64+
[CmdletBinding()]
65+
[OutputType([System.Collections.Hashtable])]
66+
param (
67+
[Parameter(Mandatory = $true)]
68+
[System.String]
69+
$InstanceName
70+
)
71+
72+
$instanceNamesRegistryKey = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\RS"
73+
74+
if (Get-ItemProperty -Path $instanceNamesRegistryKey -Name $InstanceName -ErrorAction SilentlyContinue) {
75+
$instanceId = (Get-ItemProperty -Path $instanceNamesRegistryKey -Name $InstanceName).$InstanceName
76+
77+
if (Test-Path -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instanceId\MSSQLServer\CurrentVersion") {
78+
# SQL Server 2017 SSRS stores current SQL Server version to a different Registry path.
79+
$sqlVersion = [int]((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$InstanceId\MSSQLServer\CurrentVersion" -Name "CurrentVersion").CurrentVersion).Split(".")[0]
80+
}
81+
else {
82+
$sqlVersion = [int]((Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instanceId\Setup" -Name "Version").Version).Split(".")[0]
83+
}
84+
85+
$reportingServicesConfiguration = Get-CimInstance -ClassName MSReportServer_ConfigurationSetting -Namespace "root\Microsoft\SQLServer\ReportServer\RS_$InstanceName\v$sqlVersion\Admin"
86+
$reportingServicesConfiguration = $reportingServicesConfiguration | Where-Object -FilterScript {
87+
$_.InstanceName -eq $InstanceName
88+
}
89+
90+
<#
91+
SQL Server Reporting Services Web Portal application name changed
92+
in SQL Server 2016.
93+
https://docs.microsoft.com/en-us/sql/reporting-services/breaking-changes-in-sql-server-reporting-services-in-sql-server-2016
94+
#>
95+
if ($sqlVersion -ge 13) {
96+
$reportsApplicationName = "ReportServerWebApp"
97+
}
98+
else {
99+
$reportsApplicationName = "ReportManager"
100+
}
101+
}
102+
103+
Return @{
104+
Configuration = $reportingServicesConfiguration
105+
ReportsApplicationName = $reportsApplicationName
106+
SqlVersion = $sqlVersion
107+
}
108+
}
109+
110+
function Invoke-RsCimMethod {
111+
[CmdletBinding()]
112+
[OutputType([Microsoft.Management.Infrastructure.CimMethodResult])]
113+
param (
114+
[Parameter(Mandatory = $true)]
115+
[Microsoft.Management.Infrastructure.CimInstance]
116+
$CimInstance,
117+
118+
[Parameter(Mandatory = $true)]
119+
[System.String]
120+
$MethodName,
121+
122+
[Parameter()]
123+
[System.Collections.Hashtable]
124+
$Arguments
125+
)
126+
127+
$invokeCimMethodParameters = @{
128+
MethodName = $MethodName
129+
ErrorAction = "Stop"
130+
}
131+
132+
if ($PSBoundParameters.ContainsKey("Arguments")) {
133+
$invokeCimMethodParameters["Arguments"] = $Arguments
134+
}
135+
136+
$invokeCimMethodResult = $CimInstance | Invoke-CimMethod @invokeCimMethodParameters
137+
138+
if ($invokeCimMethodResult -and $invokeCimMethodResult.HRESULT -ne 0) {
139+
if ($invokeCimMethodResult | Get-Member -Name "ExtendedErrors") {
140+
$errorMessage = $invokeCimMethodResult.ExtendedErrors -join ";"
141+
}
142+
else {
143+
$errorMessage = $invokeCimMethodResult.Error
144+
}
145+
146+
Throw "Method {0}() failed with an error. Error: {1} (HRESULT:{2})" -f @(
147+
$MethodName
148+
$errorMessage
149+
$invokeCimMethodResult.HRESULT
150+
)
151+
}
152+
153+
Return $invokeCimMethodResult
154+
155+
}

0 commit comments

Comments
 (0)