Skip to content

Commit 0e1dc2a

Browse files
wrthmnAlexander Sholokhov
andauthored
Updated Set-RsDatabase and Set-RsDatabaseCredentials scripts to support new parameters of Invoke-Sqlcmd. (#401)
* Added new parameters to Set-RsDatabase script according to SQL Strict Connection Encryption update. * Improved Set-RsDatabase script compatibility with different versions of Invoke-Sqlcmd cmdlet. * Applied the changes from Set-RsDatabase script to Set-RsDatabaseCredentials. * Moved Invoke-Sqlcmd general parameters composition to a separated region. * Enhanced -Encrypt parameter description. --------- Co-authored-by: Alexander Sholokhov <v-asholokhov@microsoft.com>
1 parent 6a1b35c commit 0e1dc2a

File tree

2 files changed

+134
-7
lines changed

2 files changed

+134
-7
lines changed

ReportingServicesTools/Functions/Admin/Set-RsDatabase.ps1

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ function Set-RsDatabase
3838
.PARAMETER DatabaseServerName
3939
Specify the database server name. (e.g. localhost, MyMachine\Sql2016, etc.)
4040
41+
.PARAMETER Encrypt
42+
Specify the encryption type to use when connecting to SQL Server.
43+
Accepted values: Mandatory, Optional, Strict.
44+
IMPORTANT: If supported by the Invoke-Sqlcmd cmdlet version in use, but not specified, the default value is Mandatory.
45+
Using this parameter requires PowerShell SQLServer module version 22 or higher.
46+
47+
.PARAMETER TrustServerCertificate
48+
Specify this switch to bypass the server certificate validation.
49+
Using this parameter requires PowerShell SQLServer module version 22 or higher.
50+
51+
.PARAMETER HostNameInCertificate
52+
Specify the host name to be used in validating the SQL Server TLS/SSL certificate.
53+
Using this parameter requires PowerShell SQLServer module version 22 or higher.
54+
4155
.PARAMETER IsRemoteDatabaseServer
4256
Specify this switch if the database server is on a different machine than the machine Reporting Services is running on.
4357
@@ -86,6 +100,16 @@ function Set-RsDatabase
86100
[string]
87101
$DatabaseServerName,
88102

103+
[ValidateSet("Mandatory", "Optional", "Strict")]
104+
[string]
105+
$Encrypt,
106+
107+
[switch]
108+
$TrustServerCertificate,
109+
110+
[string]
111+
$HostNameInCertificate,
112+
89113
[switch]
90114
$IsRemoteDatabaseServer,
91115

@@ -133,6 +157,14 @@ function Set-RsDatabase
133157
{
134158
$rsWmiObject = New-RsConfigurationSettingObjectHelper -BoundParameters $PSBoundParameters
135159

160+
$supportSQLServerV22Parameters = (Get-InstalledModule -Name "SQLServer" -MinimumVersion 22.0 -ErrorAction SilentlyContinue) -ne $null
161+
$containsSQLServerV22Parameters = $PSBoundParameters.ContainsKey("Encrypt") -or $TrustServerCertificate -or $PSBoundParameters.ContainsKey("HostNameInCertificate")
162+
163+
if ($containsSQLServerV22Parameters -and -not $supportSQLServerV22Parameters)
164+
{
165+
throw "The current version of Invoke-Sqlcmd cmdlet used in this script doesn't support -Encrypt, -TrustServerCertificate and -HostNameInCertificate parameters. Consider installing SQLServer module version 22 or higher and restarting PowerShell to use the script with these parameters."
166+
}
167+
136168
#region Validating authentication and normalizing credentials
137169
$username = ''
138170
$password = $null
@@ -178,6 +210,37 @@ function Set-RsDatabase
178210
}
179211
#endregion Validating admin authentication and normalizing credentials
180212

213+
#region Composing general parameters for Invoke-Sqlcmd cmdlet
214+
$generalParameters = @{
215+
ServerInstance = $DatabaseServerName
216+
QueryTimeout = $QueryTimeout
217+
ErrorAction = "Stop"
218+
}
219+
220+
if ($isSQLAdminAccount)
221+
{
222+
$generalParameters.add("Username", $adminUsername)
223+
$generalParameters.add("Password", $adminPassword)
224+
}
225+
226+
if ($containsSQLServerV22Parameters)
227+
{
228+
if ($PSBoundParameters.ContainsKey("Encrypt"))
229+
{
230+
$generalParameters.add("Encrypt", $Encrypt)
231+
}
232+
233+
if ($TrustServerCertificate)
234+
{
235+
$generalParameters.add("TrustServerCertificate", $true)
236+
}
237+
238+
if ($PSBoundParameters.ContainsKey("HostNameInCertificate"))
239+
{
240+
$generalParameters.add("HostNameInCertificate", $HostNameInCertificate)
241+
}
242+
}
243+
#endregion Composing general parameters for Invoke-Sqlcmd cmdlet
181244

182245
#region Create Database if necessary
183246
if (-not $IsExistingDatabase)
@@ -202,13 +265,13 @@ function Set-RsDatabase
202265
Write-Verbose "Executing database creation script..."
203266
try
204267
{
205-
if ($isSQLAdminAccount)
268+
if ($supportSQLServerV22Parameters)
206269
{
207-
Invoke-Sqlcmd -ServerInstance $DatabaseServerName -Query $SQLScript -QueryTimeout $QueryTimeout -ErrorAction Stop -Username $adminUsername -Password $adminPassword
270+
SQLServer\Invoke-Sqlcmd @generalParameters -Query $SQLScript
208271
}
209272
else
210273
{
211-
Invoke-Sqlcmd -ServerInstance $DatabaseServerName -Query $SQLScript -QueryTimeout $QueryTimeout -ErrorAction Stop
274+
Invoke-Sqlcmd @generalParameters -Query $SQLScript
212275
}
213276
}
214277
catch
@@ -240,13 +303,13 @@ function Set-RsDatabase
240303
Write-Verbose "Executing database rights script..."
241304
try
242305
{
243-
if ($isSQLAdminAccount)
306+
if ($supportSQLServerV22Parameters)
244307
{
245-
Invoke-Sqlcmd -ServerInstance $DatabaseServerName -Query $SQLScript -QueryTimeout $QueryTimeout -ErrorAction Stop -Username $adminUsername -Password $adminPassword
308+
SQLServer\Invoke-Sqlcmd @generalParameters -Query $SQLScript
246309
}
247310
else
248311
{
249-
Invoke-Sqlcmd -ServerInstance $DatabaseServerName -Query $SQLScript -QueryTimeout $QueryTimeout -ErrorAction Stop
312+
Invoke-Sqlcmd @generalParameters -Query $SQLScript
250313
}
251314
}
252315
catch

ReportingServicesTools/Functions/Admin/Set-RsDatabaseCredentials.ps1

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ function Set-RsDatabaseCredentials
1717
Specify the credentials to use when connecting to the SQL Server.
1818
Note: This parameter will be ignored whenever DatabaseCredentialType is set to ServiceAccount!
1919
20+
.PARAMETER Encrypt
21+
Specify the encryption type to use when connecting to SQL Server.
22+
Accepted values: Mandatory, Optional, Strict.
23+
If supported, but not specified, the default value is Mandatory.
24+
Using this parameter requires PowerShell SQLServer module version 22 or higher.
25+
26+
.PARAMETER TrustServerCertificate
27+
Specify this switch to bypass the server certificate validation.
28+
Using this parameter requires PowerShell SQLServer module version 22 or higher.
29+
30+
.PARAMETER HostNameInCertificate
31+
Specify the host name to be used in validating the SQL Server TLS/SSL certificate.
32+
Using this parameter requires PowerShell SQLServer module version 22 or higher.
33+
2034
.PARAMETER IsRemoteDatabaseServer
2135
Specify this parameter when the database server is on a different machine than the machine Reporting Services is on.
2236
@@ -67,6 +81,16 @@ function Set-RsDatabaseCredentials
6781
[System.Management.Automation.PSCredential]
6882
$DatabaseCredential,
6983

84+
[ValidateSet("Mandatory", "Optional", "Strict")]
85+
[string]
86+
$Encrypt,
87+
88+
[switch]
89+
$TrustServerCertificate,
90+
91+
[string]
92+
$HostNameInCertificate,
93+
7094
[switch]
7195
$IsRemoteDatabaseServer,
7296

@@ -92,6 +116,14 @@ function Set-RsDatabaseCredentials
92116
{
93117
$rsWmiObject = New-RsConfigurationSettingObjectHelper -BoundParameters $PSBoundParameters
94118

119+
$supportSQLServerV22Parameters = (Get-InstalledModule -Name "SQLServer" -MinimumVersion 22.0 -ErrorAction SilentlyContinue) -ne $null
120+
$containsSQLServerV22Parameters = $PSBoundParameters.ContainsKey("Encrypt") -or $TrustServerCertificate -or $PSBoundParameters.ContainsKey("HostNameInCertificate")
121+
122+
if ($containsSQLServerV22Parameters -and -not $supportSQLServerV22Parameters)
123+
{
124+
throw "The current version of Invoke-Sqlcmd cmdlet used in this script doesn't support -Encrypt, -TrustServerCertificate and -HostNameInCertificate parameters. Consider installing SQLServer module version 22 or higher and restarting PowerShell to use the script with these parameters."
125+
}
126+
95127
#region Validating authentication and normalizing credentials
96128
$username = ''
97129
$password = $null
@@ -135,7 +167,39 @@ function Set-RsDatabaseCredentials
135167
Write-Verbose "Executing database rights script..."
136168
try
137169
{
138-
Invoke-Sqlcmd -ServerInstance $DatabaseServerName -Query $SQLscript -QueryTimeout $QueryTimeout -ErrorAction Stop
170+
$parameters = @{
171+
ServerInstance = $DatabaseServerName
172+
Query = $SQLScript
173+
QueryTimeout = $QueryTimeout
174+
ErrorAction = "Stop"
175+
}
176+
177+
if ($containsSQLServerV22Parameters)
178+
{
179+
if ($PSBoundParameters.ContainsKey("Encrypt"))
180+
{
181+
$parameters.add("Encrypt", $Encrypt)
182+
}
183+
184+
if ($TrustServerCertificate)
185+
{
186+
$parameters.add("TrustServerCertificate", $true)
187+
}
188+
189+
if ($PSBoundParameters.ContainsKey("HostNameInCertificate"))
190+
{
191+
$parameters.add("HostNameInCertificate", $HostNameInCertificate)
192+
}
193+
}
194+
195+
if ($supportSQLServerV22Parameters)
196+
{
197+
SQLServer\Invoke-Sqlcmd @parameters
198+
}
199+
else
200+
{
201+
Invoke-Sqlcmd @parameters
202+
}
139203
}
140204
catch
141205
{

0 commit comments

Comments
 (0)