Automate SQL Server Installations

  • Hello 

    With Devops and moving to AWS being the in thing these days  , I wanted to know if any one of you can share PS scripts to automate SQL Server Installations  , say Active Passive Cluster configuration, Always on AG, FCI and standalone if needed.

  • There isn't really much to the PoSh side of things. The main thing you need is to have the configuration file for your server available. That is very much unique to your own environment. For example, my unattended install looks a like this. Note, it does ask the user for the Windows Installation disc first, so it can ensure that .Net Framework 3.5 is installed (which only takes a couple of minutes at most), and then installs SQL.
    #Create Message box object
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
    $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
    #Ask the user to confirm the Windows Installation disc (for .Net 3.5) is inserted
    $caption = "Installation Disc Required"
    $message = "Have you inserted the Windows Installation Disc?"
    $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)
    #Install .net 3.5 if yes
    if($result -eq 0) { Install-WindowsFeature Net-Framework-Core -source D:\sources\sxs }
    if($result -eq 1) {
        Write-Host "Installation of .Net Framework 3.5 Aborted."
        #The user cancelled, check they still want to progress to SQL
        $caption = "Install SQL Server"
        $message = ".NetFramework 3.5 installation was aborted. Did you want to continue to install SQL Server 2012?"
        $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)
    }

    if($result -eq 0) {
        #Ask the user to confirm that SQL Server's ISO is inserted.
        $caption = "Installation Disc Required"
        $message = "Have you inserted the SQL Server 2012 Installation Disc?"
        $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)
        #Install if yes.
        if($result -eq 0) {Start-Process "D:\setup.exe" -ArgumentList "/ConfigurationFile=C:\SQLConfigurationFile.ini" -Wait} #Note the use of the argument ConfigurationFile
        #Cancel if no.
        if($result -eq 1) { Write-Host "Installation of SQL Server 2012 Aborted." }
    }

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • The script I use isn't quite as sophisticated as Thom's.  However, what I do is keep the options in the config file to an absolute minimum, only specifying what has to be done at install time.  For everything else, I've got a T-SQL script that does everything from adding trace flags to setting max server memory.

    John

  • John does raise a good point here. I might be wrong, but unless I recall incorrectly sqlcmd is shipped with SSMS not SQL server. And SSMS is not shipped with SQL Server anymore (well, 2016 onwards). I am remembering this from memory, so if I am wrong, then please let me know.

    This does mean, that you will either need to create a PoSh to install SSMS with SQLCMD, or just SQLCMD, it depends what you want to do. I've very much moved to using SQLCMD on the servers now, as it uses a lot less resources, and use SSMS when I'm at the desktop.

    You could alternatively use SQLPS, which does come with SQL Server, HOWEVER, last I used it it was only available in 32bit, not 64.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • I'm not sure whether sqlcmd comes with the full product or just with SSMS either, to be honest.  But, despite the hollers of protest, we don't install SSMS on servers, because that encourages people to RDP on to the servers, consuming valuable resources and taking up connections that people who need to do actual admin on the server could use.

    John

  • Thom A - Monday, July 31, 2017 2:32 AM

    There isn't really much to the PoSh side of things. The main thing you need is to have the configuration file for your server available. That is very much unique to your own environment. For example, my unattended install looks a like this. Note, it does ask the user for the Windows Installation disc first, so it can ensure that .Net Framework 3.5 is installed (which only takes a couple of minutes at most), and then installs SQL.
    #Create Message box object
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
    $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
    #Ask the user to confirm the Windows Installation disc (for .Net 3.5) is inserted
    $caption = "Installation Disc Required"
    $message = "Have you inserted the Windows Installation Disc?"
    $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)
    #Install .net 3.5 if yes
    if($result -eq 0) { Install-WindowsFeature Net-Framework-Core -source D:\sources\sxs }
    if($result -eq 1) {
        Write-Host "Installation of .Net Framework 3.5 Aborted."
        #The user cancelled, check they still want to progress to SQL
        $caption = "Install SQL Server"
        $message = ".NetFramework 3.5 installation was aborted. Did you want to continue to install SQL Server 2012?"
        $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)
    }

    if($result -eq 0) {
        #Ask the user to confirm that SQL Server's ISO is inserted.
        $caption = "Installation Disc Required"
        $message = "Have you inserted the SQL Server 2012 Installation Disc?"
        $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)
        #Install if yes.
        if($result -eq 0) {Start-Process "D:\setup.exe" -ArgumentList "/ConfigurationFile=C:\SQLConfigurationFile.ini" -Wait} #Note the use of the argument ConfigurationFile
        #Cancel if no.
        if($result -eq 1) { Write-Host "Installation of SQL Server 2012 Aborted." }
    }

    maybe make this a little more dynamic by querying the disks and obtaining the drive letter for the CD drive rather than assuming it's drive D

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • Perry Whittle - Monday, July 31, 2017 8:17 AM

    maybe make this a little more dynamic by querying the disks and obtaining the drive letter for the CD drive rather than assuming it's drive D

    Part of our server set up is that D: is the Disc drive. Means it's standard across them all.

    Every individual has their own needs, that's just an exert from my script. It also assumes the location of the configuration file as well. The OP might want to allow for a dialog to input this. It's just getting them on the right foot. 🙂

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply