|
1 | | -param( |
2 | | - [Parameter()] |
| 1 | +param( |
| 2 | + [Parameter()] |
3 | 3 | [String]$SqlInstance = $env:DB_INSTANCE, |
4 | 4 | [String]$Database = $env:TARGET_DB, |
5 | | - [String]$CLRScript = "tests\tSQLt\SetClrEnabled.sql", |
6 | | - [String]$CreateDBScript = "tests\tSQLt\CreateDatabase.sql", |
7 | | - [String]$tSQLtInstallScript = "tests\tSQLt\tSQLt.class.sql", |
8 | 5 | [String]$Color = "Green", |
9 | | - [String]$Master = "master", |
10 | 6 | [string]$User = $env:AZURE_SQL_USER, |
11 | 7 | [string]$Pass = $env:AZURE_SQL_PASS, |
12 | 8 | [bool]$IsAzureSQL = [System.Convert]::ToBoolean($env:AzureSQL) |
13 | 9 | ) |
14 | 10 |
|
15 | | -Write-Host "Installing tSQLt..." -ForegroundColor $Color |
| 11 | +Write-Host "Downloading and installing tSQLt..." -ForegroundColor $Color |
| 12 | + |
| 13 | +# BaseUrl gets the latest version by default - blocked by https://github.com/LowlyDBA/dba-multitool/issues/165 |
| 14 | +$Version = "1-0-5873-27393" |
| 15 | +$DownloadUrl = "http://tsqlt.org/download/tsqlt/?version=" + $Version |
| 16 | +$TempPath = [System.IO.Path]::GetTempPath() |
| 17 | +$ZipFile = Join-Path $TempPath "tSQLt.zip" |
| 18 | +$ZipFolder = Join-Path $TempPath "tSQLt" |
| 19 | +#$SetupFile = Join-Path $ZipFolder "PrepareServer.sql" # Used in latest version after 1.0.5873.27393 |
| 20 | +$SetupFile = Join-Path $ZipFolder "SetClrEnabled.sql" |
| 21 | +$InstallFile = Join-Path $ZipFolder "tSQLt.class.sql" |
| 22 | +$CreateDbQuery = "CREATE DATABASE [tSQLt];" |
| 23 | +$CLRSecurityQuery = " |
| 24 | +/* Turn off CLR Strict for 2017+ fix */ |
| 25 | +IF EXISTS (SELECT 1 FROM sys.configurations WHERE name = 'clr strict security') |
| 26 | +BEGIN |
| 27 | + EXEC sp_configure 'show advanced options', 1; |
| 28 | + RECONFIGURE; |
| 29 | + |
| 30 | + EXEC sp_configure 'clr strict security', 0; |
| 31 | + RECONFIGURE; |
| 32 | +END |
| 33 | +GO" |
| 34 | + |
| 35 | + |
| 36 | +# Download |
| 37 | +Try { |
| 38 | + Invoke-WebRequest -Uri $DownloadUrl -OutFile $ZipFile -ErrorAction Stop -UseBasicParsing |
| 39 | + Expand-Archive -Path $ZipFile -DestinationPath $ZipFolder -Force |
| 40 | +} |
| 41 | + |
| 42 | +Catch { |
| 43 | + Write-Error -Message "Error downloading tSQLt - try manually fetching from $DownloadUrl" |
| 44 | +} |
16 | 45 |
|
17 | 46 | $Hash = @{ |
18 | 47 | SqlInstance = $SqlInstance |
19 | | - Database = $Database |
| 48 | + Database = $Database |
20 | 49 | EnableException = $true |
21 | 50 | } |
22 | 51 |
|
| 52 | +# Setup |
23 | 53 | If ($IsAzureSQL) { |
24 | 54 | $SecPass = ConvertTo-SecureString -String $Pass -AsPlainText -Force |
25 | 55 | $Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $SecPass |
26 | 56 | $Hash.add("SqlCredential", $Credential) |
27 | | - |
28 | | - Invoke-DbaQuery @hash -File $tSQLtInstallScript |
29 | 57 | } |
30 | 58 |
|
31 | 59 | Else { |
32 | | - Invoke-DbaQuery -SqlInstance $SqlInstance -Database $Master -File $clrscript | Out-Null |
33 | | - Invoke-DbaQuery -SqlInstance $SqlInstance -Database $Master -File $CreateDBScript | Out-Null |
34 | | - Invoke-DbaQuery @Hash -File $tSQLtInstallScript -MessagesToOutput |
| 60 | + Invoke-DbaQuery -SqlInstance $SqlInstance -Database "master" -Query $CreateDbQuery |
| 61 | + # DbaQuery doesn't play nice with the setup script GOs - default back to sqlcmd |
| 62 | + Invoke-Command -ScriptBlock { sqlcmd -S $SqlInstance -d $Database -i $SetupFile } | Out-Null |
| 63 | + Invoke-DbaQuery @Hash -Query $CLRSecurityQuery |
35 | 64 | } |
| 65 | + |
| 66 | +# Install |
| 67 | +Invoke-DbaQuery @Hash -File $InstallFile |
0 commit comments