Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
MSSQL Server Setup - designed to be run via ssh, ie vagrant under opentable/win-2012r2-standard-amd64-nocm
$ErrorActionPreference = "Stop"
$client = new-object System.Net.WebClient
# Reset vagrant password so it's not expired
# Setup UAC wrapper ;(
if(!(Test-Path -Path "C:\")) {
Write-Output "Setting up UAC wrapper"
$client.DownloadFile("", "C:\")
$shell = new-object -com shell.application
$zip = $shell.NameSpace("C:\")
foreach($item in $zip.items())
# Install
$p = [System.Diagnostics.Process]::Start('C:\setup.exe')
# Disable the firewall
Write-Output "Disabling the firewall"
netsh advfirewall set allprofiles state off
# Install .NET
Write-Output "Installing .NET"
import-module servermanager
add-windowsfeature as-net-framework
# Download SQL server
if(!(Test-Path -Path "C:\SQLEXPR_x64_ENU.exe")) {
Write-Output "Downloading SQL server"
$client.DownloadFile("", "C:\SQLEXPR_x64_ENU.exe")
# Install SQL server
Write-Output "Installing SQL server"
# This crap is to work around UAC and no admin rights authing via ssh key
$service = new-object -ComObject("Schedule.Service")
$TaskDefinition = $service.NewTask(0)
$TaskDefinition.RegistrationInfo.Description = "Install SQL server"
$TaskDefinition.Settings.Enabled = $true
$TaskDefinition.Settings.AllowDemandStart = $true
$triggers = $TaskDefinition.Triggers
$trigger = $triggers.Create(1)
$trigger.StartBoundary = [datetime]::Now.AddMinutes(0.5).ToString("yyyy-MM-dd'T'HH:mm:ss")
$trigger.Enabled = $true
$Action = $TaskDefinition.Actions.Create(0)
$action.Path = 'C:\SQLEXPR_x64_ENU.exe'
$rootFolder = $service.GetFolder("\")
$rootFolder.RegisterTaskDefinition("Install SQL server",$TaskDefinition,6,"System",$null,5)
# Wait for SQL server install to finish
while ($true) {
if (Get-Service 'MSSQL$SQLEXPRESS' -ErrorAction SilentlyContinue | Where-Object {$_.status -eq "running"}) {
# Wait 10 seconds
Write-Output "Waiting for SQL server install to finish...."
Start-Sleep -s 10
# Wait to make sure everything is in place
Start-Sleep -s 10
Write-Output "SQL server install finished, setting up port"
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | Out-Null
$MachineObject = new-object ('Microsoft.SqlServer.Management.Smo.WMI.ManagedComputer') .
$ProtocolUri = "ManagedComputer[@Name='" + (get-item env:\computername).Value + "']/ServerInstance[@Name='SQLEXPRESS']/ServerProtocol"
$tcp = $MachineObject.getsmoobject($ProtocolUri + "[@Name='Tcp']")
$tcp.IsEnabled = $true
Start-Sleep -s 2
restart-service -f "SQL Server (SQLEXPRESS)"
Write-Output "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment