Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install SQL Server from network share ISO with pre-configured INI file, prompting for service account credentials
# AUTHOR: Nate Johnson, @njohnson9402/@natethedba, natethedba.wordpress.com
# LICENSE: https://choosealicense.com/licenses/unlicense/
# TYPE: PowerShell script
# DESCRIPTION/USAGE:
# There are TWO static placeholders that you need to change/type-in when you want to use this,
# they both start with <PATH TO ...>. I also added a #CHANGE THIS! comment to the end of their lines.
# The first one is for your installer config .ini file
# (see other gist at https://gist.github.com/NJohnson9402/a3c13429a055771efd26eefa66c69d62).
# The second is for the location of your SQL server installation media (ISO).
# See corresponding blog post at https://natethedba.wordpress.com/automating-sql-installation/
# get account credentials to configure SQL
$sqlsvc = Get-Credential -Message "SQL Service account?"
$agtsvc = Get-Credential -Message "SQL Agent account?"
$sapwd = $(Read-Host -Prompt "SQL 'sa' login pwd" -AsSecureString)
# multi-line string concatenation
$arglist = '/ConfigurationFile="<PATH TO your config file>.ini"' ` #CHANGE THIS!
+ ' /AGTSVCACCOUNT="' + $agtsvc.UserName + '"' `
+ ' /AGTSVCPASSWORD="' + $agtsvc.GetNetworkCredential().Password + '"' `
+ ' /SQLSVCACCOUNT="' + $sqlsvc.UserName + '"' `
+ ' /SQLSVCPASSWORD="' + $sqlsvc.GetNetworkCredential().Password + '"' `
+ ' /SAPWD="' + [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($sapwd)) + '"'
# this last crazy tidbit converts the SecureString to a plain string that is still "secure"
# in the sense that it can't be sniffed or dumped from memory by another process. At least, that's my understanding.
# mount the install image
$iso = Get-ChildItem -Path "\\<PATH TO your SQL Server 2016 Standard x64 iso folder>\" #CHANGE THIS!
Mount-DiskImage $iso.FullName
# get the drive letter of the mounted image to reference setup.exe
$setup = $(Get-DiskImage -ImagePath $iso.FullName | Get-Volume).DriveLetter + ":\setup.exe"
# run installer with arg-list built above, including config file and service/SA accounts
Start-Process -Verb runas -FilePath $setup -ArgumentList $arglist -Wait
# un-mount the install image when done after waiting 1 second (just for kicks)
Start-Sleep -Seconds 1
Dismount-DiskImage $iso.FullName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.