11param (
22 [ValidateSet (" sqlcmd" , " sqlengine" )]
33 [string []]$Components ,
4+ [bool ]$ForceEncryption ,
45 [string ]$SaPassword ,
56 [ValidateSet (" 2017" )]
67 [string ]$Version
910function Wait-ForContainer {
1011 $checkInterval = 5
1112 $containerName = " sql"
12- $timeout = 120
13+ $timeout = 60
1314
1415 $startTime = Get-Date
1516 Write-Host " Waiting for the container '$containerName ' to be healthy..."
@@ -73,8 +74,31 @@ if ("sqlengine" -in $Components) {
7374 exit 1
7475 }
7576
77+ if ($ForceEncryption ) {
78+ Write-Output " Force encryption is set, generating self-signed certificate ..."
79+
80+ # SOURCE: https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-docker-container-security?view=sql-server-ver16#encrypt-connections-to-sql-server-linux-containers
81+ & mkdir - p / opt/ mssql
82+ & openssl req - x509 - nodes - newkey rsa:2048 - subj ' /CN=sql1.contoso.com' - keyout / opt/ mssql/ mssql.key - out / opt/ mssql/ mssql.pem - days 365
83+ $MssqlConf = @'
84+ [network]
85+ tlscert = /etc/ssl/certs/mssql.pem
86+ tlskey = /etc/ssl/private/mssql.key
87+ tlsprotocols = 1.2
88+ forceencryption = 1
89+ '@
90+
91+ Set-Content - Path / opt/ mssql/ mssql.conf - Value $MssqlConf
92+ & sudo chmod - R 775 / opt/ mssql
93+
94+ Copy-Item - Path / opt/ mssql/ mssql.pem - Destination / usr/ share/ ca- certificates/ mssql.crt
95+ & sudo dpkg- reconfigure ca- certificates
96+
97+ $AdditionalContainerConfiguration = " -v /opt/mssql/mssql.conf:/var/opt/mssql/mssql.conf -v /opt/mssql/mssql.pem:/etc/ssl/certs/mssql.pem -v /opt/mssql/mssql.key:/etc/ssl/private/mssql.key"
98+ }
99+
76100 Write-Output " Starting a Docker Container"
77- Invoke-Expression " docker run --name=`" sql`" -e `" ACCEPT_EULA=Y`" -e `" SA_PASSWORD=$SaPassword `" -e `" MSSQL_PID=Express`" --health-cmd=`" /opt/mssql-tools/bin/sqlcmd -C -S localhost -U sa -P '$SaPassword ' -Q 'SELECT 1' -b -o /dev/null`" --health-start-period=`" 10s`" --health-retries=3 --health-interval=`" 10s`" -p 1433:1433 -d `" mcr.microsoft.com/mssql/server:$Version -latest`" "
101+ Invoke-Expression " docker run --name=`" sql`" -e `" ACCEPT_EULA=Y`" -e `" SA_PASSWORD=$SaPassword `" -e `" MSSQL_PID=Express`" --health-cmd=`" /opt/mssql-tools/bin/sqlcmd -C -S localhost -U sa -P '$SaPassword ' -Q 'SELECT 1' -b -o /dev/null`" --health-start-period=`" 10s`" --health-retries=3 --health-interval=`" 10s`" -p 1433:1433 $AdditionalContainerConfiguration -d `" mcr.microsoft.com/mssql/server:$Version -latest`" "
78102 Wait-ForContainer
79103 }
80104
@@ -88,9 +112,26 @@ if ("sqlengine" -in $Components) {
88112
89113 Write-Host " Configuring SQL Express ..."
90114 stop-service MSSQL`$ SQLEXPRESS
91- set-itemproperty - path ' HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' - name tcpdynamicports - value ' '
92- set-itemproperty - path ' HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\supersocketnetlib\tcp\ipall' - name tcpport - value 1433
93- set-itemproperty - path ' HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver\' - name LoginMode - value 2
115+
116+ $InstancePath = " HKLM:\software\microsoft\microsoft sql server\mssql14.SQLEXPRESS\mssqlserver"
117+ $SuperSocketNetLibPath = " $InstancePath \supersocketnetlib"
118+ set-itemproperty - path " $SuperSocketNetLibPath \tcp\ipall" - name tcpdynamicports - value ' '
119+ set-itemproperty - path " $SuperSocketNetLibPath \tcp\ipall" - name tcpport - value 1433
120+ set-itemproperty - path $InstancePath - name LoginMode - value 2
121+
122+ # SOURCE: https://blogs.infosupport.com/configuring-sql-server-encrypted-connections-using-powershell/
123+ if ($ForceEncryption ) {
124+ Write-Output " Force encryption is set, configuring SQL server to do so ..."
125+
126+ $params = @ {
127+ DnsName = ' sql1.contoso.com'
128+ CertStoreLocation = ' Cert:\LocalMachine\My'
129+ }
130+ $Certificate = New-SelfSignedCertificate @params
131+
132+ Set-ItemProperty $SuperSocketNetLibPath - Name " Certificate" - Value $Certificate.Thumbprint.ToLowerInvariant ()
133+ Set-ItemProperty $SuperSocketNetLibPath - Name " ForceEncryption" - Value 1
134+ }
94135
95136 Write-Host " Starting SQL Express ..."
96137 start-service MSSQL`$ SQLEXPRESS
0 commit comments