setup-sqlserver.ps1 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # This script downloads and installs SQL Server and opens it on port 1433.
  2. #
  3. # To run this script, run an elevated Command Prompt and enter:
  4. #
  5. # powershell -ExecutionPolicy Bypass -File <this script's filename>
  6. $basedir = "C:\FrameworkBenchmarks"
  7. $workdir = "$basedir\installs"
  8. New-Item -Path $workdir -Type directory -Force | Out-Null
  9. If (-Not (Get-Service | ? Name -Eq "MSSQLSERVER")) {
  10. Write-Host "Could not find default SQL Server instance, MSSQLSERVER."
  11. Write-Host "Downloading SQL Server (several GBs)..."
  12. # URLs from http://www.microsoft.com/en-us/download/details.aspx?id=35575
  13. $sqlserver_exe_url = "http://download.microsoft.com/download/3/B/D/3BD9DD65-D3E3-43C3-BB50-0ED850A82AD5/SQLServer2012SP1-FullSlipstream-x64-ENU.exe"
  14. $sqlserver_exe_local = "$workdir\SQLServer2012SP1-FullSlipstream-x64-ENU.exe"
  15. (New-Object System.Net.WebClient).DownloadFile($sqlserver_exe_url, $sqlserver_exe_local)
  16. $sqlserver_box_url = "http://download.microsoft.com/download/3/B/D/3BD9DD65-D3E3-43C3-BB50-0ED850A82AD5/SQLServer2012SP1-FullSlipstream-x64-ENU.box"
  17. $sqlserver_box_local = "$workdir\SQLServer2012SP1-FullSlipstream-x64-ENU.box"
  18. (New-Object System.Net.WebClient).DownloadFile($sqlserver_box_url, $sqlserver_box_local)
  19. Write-Host "Installing SQL Server..."
  20. # Install only the SQL Server database engine.
  21. # Use a default instance name.
  22. # Make %COMPUTERNAME%\Administrators have administrative rights.
  23. # The following is not used because this is done in PowerShell below.
  24. # /securitymode=SQL /sapwd=S3cr3tS3cr3t /TCPENABLED=1
  25. # Allow Windows Authentication or old-style SQL authentication.
  26. # The password of the sa account is specified.
  27. # SQL Server will be listening on TCP port 1433.
  28. #
  29. Start-Process "$sqlserver_exe_local" "/q /action=install /features=SQLEngine /INSTANCENAME=MSSQLSERVER /SQLSYSADMINACCOUNTS=Administrators /IACCEPTSQLSERVERLICENSETERMS" -Wait
  30. }
  31. # In case we just installed SQL Server and the environment variables haven't been refreshed, manually
  32. # refresh PSModulePath so that Import-Module sqlps will work.
  33. $env:PSModulePath = [Environment]::GetEnvironmentVariable("PSModulePath", [System.EnvironmentVariableTarget]::Machine)
  34. Import-Module sqlps
  35. Write-Host "Setting SQL Server to start on boot..."
  36. Set-Service MSSQLSERVER -StartupType Automatic
  37. Write-Host "Ensuring that SQL Server is started..."
  38. Start-Service MSSQLSERVER
  39. Write-Host "Enabling SQL authentication..."
  40. # Enable SQL authentication
  41. $s = New-Object ('Microsoft.SqlServer.Management.Smo.Server')
  42. $s.Settings.LoginMode = [Microsoft.SqlServer.Management.SMO.ServerLoginMode]::Mixed
  43. $s.Alter()
  44. Write-Host "Configuring SQL Server to listen on TCP (default port 1433)..."
  45. # Enable the TCP protocol on the default instance.
  46. $wmi = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer')
  47. $uri = "ManagedComputer[@Name='" + (Get-Content env:computername) + "']/ ServerInstance[@Name='MSSQLSERVER']/ServerProtocol[@Name='Tcp']"
  48. $Tcp = $wmi.GetSmoObject($uri)
  49. $Tcp.IsEnabled = $true
  50. $Tcp.Alter()
  51. Write-Host "Restarting SQL Server..."
  52. Restart-Service -Name MSSQLSERVER
  53. If (-Not (Get-NetFirewallPortFilter | ? LocalPort -Eq "1433")) {
  54. Write-Host "Opening port 1433 in firewall..."
  55. New-NetFirewallRule -DisplayName "SQL 1433" -Action Allow -Direction Inbound -LocalPort 1433 -Protocol TCP | Out-Null
  56. } else {
  57. Write-Host "Port 1433 is already configured in firewall."
  58. }
  59. Write-Host "Creating SQL Server login and populated database..."
  60. # Connect with Windows Authentication, assuming that we have access.
  61. Invoke-Sqlcmd -InputFile "$basedir\config\create-sqlserver-login-and-database.sql" -OutputSqlErrors $True -QueryTimeout 180
  62. # Now that benchmarkdbuser has been created, we can connect with those credentials.
  63. Invoke-Sqlcmd -Username benchmarkdbuser -Password B3nchmarkDBPass -Database hello_world -InputFile "$basedir\config\create-sqlserver.sql" -OutputSqlErrors $True -QueryTimeout 180
  64. Write-Host "Done."