Files
sql-server-samples/samples/manage/windows-containers/mssql-server-2016-developer-windows/start.ps1
T
Pau 151a550d8f Update start.ps1
Enclose db names with [ ] enabling names for example with "." : MyDatabase.Develop
2016-10-02 22:40:36 +02:00

47 lines
1.2 KiB
PowerShell

# The script sets the sa password and start the SQL Service
# Also it attaches additional database from the disk
# The format for attach_dbs
param(
[Parameter(Mandatory=$false)]
[string]$sa_password,
[Parameter(Mandatory=$false)]
[string]$attach_dbs
)
# start the service
Write-Verbose "Starting SQL Server"
start-service MSSQL`$SQLDEV
if($sa_password -ne "_"){
Write-Verbose "Changing SA login credentials"
$sqlcmd = "ALTER LOGIN sa with password=" +"'" + $sa_password + "'" + ";ALTER LOGIN sa ENABLE;"
Invoke-Sqlcmd -Query $sqlcmd -ServerInstance ".\SQLDEV"
}
$attach_dbs_cleaned = $attach_dbs.TrimStart('\\').TrimEnd('\\')
$dbs = $attach_dbs_cleaned | ConvertFrom-Json
if ($null -ne $dbs -And $dbs.Length -gt 0){
Write-Verbose "Attaching $($dbs.Length) database(s)"
Foreach($db in $dbs)
{
$files = @();
Foreach($file in $db.dbFiles)
{
$files += "(FILENAME = N'$($file)')";
}
$files = $files -join ","
$sqlcmd = "sp_detach_db [$($db.dbName)];CREATE DATABASE [$($db.dbName)] ON $($files) FOR ATTACH ;"
Write-Verbose "Invoke-Sqlcmd -Query $($sqlcmd) -ServerInstance '.\SQLDEV'"
Invoke-Sqlcmd -Query $sqlcmd -ServerInstance ".\SQLDEV"
}
}
Write-Verbose "Started SQL Server."
while ($true) { Start-Sleep -Seconds 3600 }