mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
47 lines
1.3 KiB
PowerShell
47 lines
1.3 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`$SQLEXPRESS
|
|
|
|
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 ".\SQLEXPRESS"
|
|
}
|
|
|
|
$attach_dbs_cleaned = $attach_dbs.TrimStart('\\').TrimEnd('\\')
|
|
|
|
Write-Verbose "Attach Database configuration passed: $($attach_dbs_cleaned)"
|
|
|
|
$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 '.\SQLEXPRESS'"
|
|
Invoke-Sqlcmd -Query $sqlcmd -ServerInstance ".\SQLEXPRESS"
|
|
}
|
|
}
|
|
|
|
while ($true) { Start-Sleep -Seconds 3600 } |