Custom script that creates a new database if not exists and executes
a custom SQL script with a prepared variable.
File name: customcode.ps1
Import-Module /usr/local/lib/Utils.psm1 -Function "Get-EnvVar"
Import-Module /usr/local/lib/Crypto.psm1
$saPassword = Get-EnvVar "SA_PASSWORD"
$aSecret = Get-EnvVar "MYCORPORATION_SECRET"
$myCustomDatabaseName = "MYDB"
$myDatabasePath = Join-Path "/" "var" "opt" "mssql" "data" "$myCustomDatabaseName.mdf"
$myScriptPath = Join-Path "/" "usr" "local" "mycorporation" "scripts" "myscript.sql"
ServerInstance = "localhost"
if (-not (Test-Path $myDatabasePath)) {
$secretSalt = Get-Salt # from Crypto.psm1
$secretPassHash = Get-PasswordHash ($salt + $aSecret)
Invoke-Sqlcmd "CREATE DATABASE [`$(DATABASE_NAME)] CONTAINMENT = PARTIAL" `
-Variable ,"DATABASE_NAME=$myCustomDatabaseName" `
Invoke-Sqlcmd -InputFile $myScriptPath `
-Variable ,"A_SECRET=$secretPassHash" `
# Restart the SQL Server process so that the logs
# are written in the standard output file
Stop-Process -Name sqlservr -Force
Start-Process -FilePath /opt/mssql/bin/sqlservr -Wait