Updated Set-RsDatabase and Set-RsDatabaseCredentials scripts to support new parameters of Invoke-Sqlcmd.#401
Conversation
… Connection Encryption update.
…of Invoke-Sqlcmd cmdlet.
@microsoft-github-policy-service agree company="Akvelon" |
| $parameters.add("Password", $adminPassword) | ||
| } | ||
|
|
||
| if ($containsSQLServerV22Parameters) |
There was a problem hiding this comment.
this code is duplicated twice in this cmdlet consider moving up so its only done once
There was a problem hiding this comment.
Moved Invoke-Sqlcmd general parameters composition to a separated region.
| .PARAMETER Encrypt | ||
| Specify the encryption type to use when connecting to SQL Server. | ||
| Accepted values: Mandatory, Optional, Strict. | ||
| If supported, but not specified, the default value is Mandatory. |
There was a problem hiding this comment.
please enhance the comment mentioning what need to support it
If supported by?
There was a problem hiding this comment.
By "If supported" I meant having the required version of SQLServer PowerShell module installed to use this parameter (as mentioned on the next line) .
Tried to make the comment more comprehensible and added IMPORTANT: warning, since the default value of this parameter is the most significant change in the Invoke-Sqlcmd (as well as the related scripts) behavior.
Motivation:
Set-RsDatabasescript started to result in failure with the following error:After investigation, I discovered that this behavior is a result of the recent update of SQL Strict Connection Encryption, which introduced some changes to the defaults of SQL connection encryption.
Since v22,
Invoke-Sqlcmdcmdlet from PowerShell SqlServer module has three new parameters:According to the documentation on Invoke-Sqlcmd cmdlet, the default value for
-Encryptparameter is nowMandatory, which seems to be the reason of the connection failure, since currently no value for-Encryptparameter is specified forInvoke-Sqlcmdinvocation inSet-RsDatabasescript.Previously the encryption of the connection depended on
-EncryptConnectionswitch ofInvoke-Sqlcmdcmdlet, which was not specified in the script and is considered deprecated since v22 of SqlServer module.So, basically, if the user has the SqlServer module v22 or higher installed, the
Set-RsDatabase/Set-RsDatabaseCredentialsscript uses the recently updated version ofInvoke-Sqlcmdcmdlet, which makes the connection Mandatory encrypted by default.I consider the ability to specify the newly introduced parameters a required improvment for
Set-RsDatabaseandSet-RsDatabaseCredentialsscripts functionality.Changes proposed in this pull request:
Added new parameters to
Set-RsDatabaseandSet-RsDatabaseCredentialsscripts according to updatedInvoke-Sqlcmdparameters:Refactored parameters specification for
Invoke-Sqlcmdinvocation.I also tried to ensure compatibility of the modified scripts with different versions of
Invoke-Sqlcmdcmdlet, since it is present in SqlServer , PsSqlLegacy and SQLPS modules:Invoke-Sqlcmdcmdlet from this module, and user will have to specify the recently introduced parameters to prevent the default encryption of the connection (-Encrypt=Optionaland-TrustServerCertificateis advised);How to test this code:
Set-RsDatabase/Set-RsDatabaseCredentialsscript in different environments (various versions of SqlServer and other modules installed/removed);Has been tested on: