-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
restore-database-for-testing.ps1
31 lines (26 loc) · 1.35 KB
/
restore-database-for-testing.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$mssqlPrefix = "mssql`$sql"
$mssqlName = (Get-Service $mssqlPrefix* | Sort-Object Name -Descending | Select-Object -first 1).Name
$mssqlVersion = $mssqlName.Substring($mssqlPrefix.Length)
Write-Host "Selected service '$mssqlName' (Version:$mssqlVersion)"
if ((Get-Service $mssqlName).Status -eq "Running")
{ Write-Host "Service '$mssqlName' is already running" }
else
{
Write-Host "Starting service '$mssqlName'"
Start-Service $mssqlName
Write-Host "Service '$mssqlName' is $((Get-Service $mssqlName).Status)"
}
Start-FileDownload "https://github.com/Microsoft/sql-server-samples/releases/download/adventureworks/AdventureWorks$mssqlVersion.bak" -FileName "c:\projects\AdventureWorks$mssqlVersion.bak"
if (Get-Module -ListAvailable -Name dbatools)
{ Write-Host "Module dbatools already installed" }
else
{
Write-Host "Installing module dbatools ..."
Install-Module dbatools -Scope CurrentUser
Write-Host "Module dbatools installed"
}
Write-Host "Switching default SQL Server connection encryption ..."
Set-DbatoolsConfig -FullName sql.connection.trustcert -Value $true
Set-DbatoolsConfig -FullName sql.connection.encrypt -Value $false
Write-Host "Restoring AdventureWorks$mssqlVersion on $env:computername\SQL$mssqlVersion ..."
Restore-DbaDatabase -SqlInstance $env:computername\SQL$mssqlVersion -Path c:\projects\AdventureWorks$mssqlVersion.bak