Cartwheel.SqlServer.psm1

#
# Cartwheel_SqlServer.psm1
#


# SQL ENGINE
function Get-SqlInstance {
    [CmdletBinding()]
    param()
        $Win32Name= "mssql*"
        $Win32Path=  "sqlservr.exe"
        $Instances = Get-WmiObject win32_service -computerName localhost | Where-Object { $_.Name -match $Win32Name -and $_.PathName -match $Win32Path } 
        return $Instances | Select Name,State
    }

function Add-SqlInstance {
    [CmdletBinding()]
    param(            
        [Parameter(Mandatory = $true)]
        [string]$InstanceName, #"MSSQLSERVER" for default

        [Parameter(Mandatory = $true)]
        [string]$MediaLocation,

        [Parameter(Mandatory = $true)]
        [string]$Features,

        [Parameter(Mandatory = $true)]
        [string]$SQLSYSADMINACCOUNTS

    )

        $ConfigLocation=Build-SqlConfigFile $InstanceName $Features $SQLSYSADMINACCOUNTS

        # determine if our media is an exe or iso
        $isISO = If ($MediaLocation.substring($MediaLocation.length - 4, 4) -eq ".iso") {$true} else {$false}
        $Command= "\Setup.exe /Action=Install /ConfigurationFile=$ConfigLocation /Q /IAcceptSQLServerLicenseTerms=true"

    
    if ($isISO -eq $true)
    {
                # mount the iso
                $setupDriveLetter = (Mount-DiskImage -ImagePath $MediaLocation -PassThru | Get-Volume).DriveLetter + ":"
                if ($setupDriveLetter -eq $null) {
                    throw "Could not mount iso"
                }
                Write-Verbose "Drive letter for iso is: $setupDriveLetter"
                  
                # run the installer
                $cmd = "$setupDriveLetter\$Command"
                Write-Verbose "Running Install ..."
                Invoke-Expression $cmd | Write-Verbose
 
                # dismount the iso
                Dismount-DiskImage -ImagePath $MediaLocation 
                Remove-Item -Recurse -Force $ConfigLocation -ErrorVariable ItemErr -ErrorAction SilentlyContinue ;

    }
    else
    {
                #if this is just and exe
                # run the installer
                $cmd = "$Command"
                Write-Verbose "Running Install ..."
                Invoke-Expression $cmd | Write-Verbose

    }
    
    
    
    }

function Remove-SqlInstance {
    [CmdletBinding()]
    param(            
        [Parameter(Mandatory = $true)]
        [string]$InstanceName, #"MSSQLSERVER" for default

        [Parameter(Mandatory = $true)]
        [string]$MediaLocation,

        [Parameter(Mandatory = $true)]
        [string]$Features

        )
    

        # determine if our media is an exe or iso
        $isISO = If ($MediaLocation.substring($MediaLocation.length - 4, 4) -eq ".iso") {$true} else {$false}
        $Command= "setup.exe /Action=Uninstall /FEATURES=$Features /INSTANCENAME=$InstanceName /Q /IACCEPTROPENLICENSETERMS=True"
    
    if ($isISO -eq $true)
    {
                # mount the iso
                $setupDriveLetter = (Mount-DiskImage -ImagePath $MediaLocation -PassThru | Get-Volume).DriveLetter + ":"
                if ($setupDriveLetter -eq $null) {
                    throw "Could not mount iso"
                }
                Write-Verbose "Drive letter for iso is: $setupDriveLetter"
                  
                # run the installer
                $cmd = "$setupDriveLetter\$Command"
                Write-Verbose "Running UnInstall ..."
                Invoke-Expression $cmd | Write-Verbose
 
                # dismount the iso
                Dismount-DiskImage -ImagePath $MediaLocation 

    }
    else
    {
                #if this is just and exe
                # run the installer
                $cmd = "$Command"
                Write-Verbose "Running UnInstall ..."
                Invoke-Expression $cmd | Write-Verbose

    }
    
        
    }

function Build-SqlConfigFile{
     [CmdletBinding()]
    param(

    #Specify a default or named instance. MSSQLSERVER is the default instance for non-Express editions and SQLExpress for Express editions. This parameter is required when installing the SQL Server Database Engine (SQL), Analysis Services (AS), or Reporting Services (RS).
    [Parameter(Mandatory = $true)]
    [string]$INSTANCENAME,    

    #Specifies features to install, uninstall, or upgrade. The list of top-level features include SQL, AS, RS, IS, MDS, and Tools. The SQL feature will install the Database Engine, Replication, Full-Text, and Data Quality Services (DQS) server. The Tools feature will install shared components.
    [Parameter(Mandatory = $true)]
    [string]$FEATURES,#="SQLENGINE,ADVANCEDANALYTICS,IS"

    #Specify the root installation directory for shared components. This directory remains unchanged after shared components are already installed.
    [Parameter(Mandatory = $true)]
    [string]$SQLSYSADMINACCOUNTS


    )

$basestring="
 
;SQL Server 2017 Configuration File
[OPTIONS]
 
IACCEPTROPENLICENSETERMS=`"True`"
ACTION=`"Install`"
SUPPRESSPRIVACYSTATEMENTNOTICE=`"False`"
 
ENU=`"True`"
QUIET=`"True`"
 
UpdateEnabled=`"True`"
USEMICROSOFTUPDATE=`"False`"
FEATURES=$FEATURES
UpdateSource=`"MU`"
HELP=`"False`"
INDICATEPROGRESS=`"False`"
X86=`"False`"
INSTANCENAME=`"$INSTANCENAME`"
INSTANCEID=`"$INSTANCENAME`"
 
INSTALLSHAREDDIR=`"C:\Program Files\Microsoft SQL Server`"
INSTALLSHAREDWOWDIR=`"C:\Program Files (x86)\Microsoft SQL Server`"
INSTANCEDIR=`"C:\Program Files\Microsoft SQL Server`"
 
 
 
    "


$sqlstring = "
 
 
SQLSYSADMINACCOUNTS=`"$SQLSYSADMINACCOUNTS`"
SQLTELSVCACCT=`"NT Service\SQLTELEMETRY`$$INSTANCENAME`"
SQLSVCACCOUNT=`"NT Service\MSSQL`$$INSTANCENAME`"
 
SQLSVCSTARTUPTYPE=`"Automatic`"
SQLTELSVCSTARTUPTYPE=`"Automatic`"
 
COMMFABRICPORT=`"0`"
COMMFABRICNETWORKLEVEL=`"0`"
COMMFABRICENCRYPTION=`"0`"
MATRIXCMBRICKCOMMPORT=`"0`"
 
FILESTREAMLEVEL=`"0`"
ENABLERANU=`"False`"
SQLCOLLATION=`"SQL_Latin1_General_CP1_CI_AS`"
 
SQLSVCINSTANTFILEINIT=`"False`"
 
SQLTEMPDBFILECOUNT=`"1`"
SQLTEMPDBFILESIZE=`"8`"
SQLTEMPDBFILEGROWTH=`"64`"
SQLTEMPDBLOGFILESIZE=`"8`"
SQLTEMPDBLOGFILEGROWTH=`"64`"
ADDCURRENTUSERASSQLADMIN=`"False`"
TCPENABLED=`"1`"
NPENABLED=`"0`"
BROWSERSVCSTARTUPTYPE=`"Disabled`"
EXTSVCACCOUNT=`"NT Service\MSSQLLaunchpad`"
"


$analyticsstring="
 
ASSVCSTARTUPTYPE=`"Automatic`"
ASTELSVCSTARTUPTYPE=`"Automatic`"
 
ASTELSVCACCT=`"NT Service\SSASTELEMETRY`$$INSTANCENAME`"
ASSVCACCOUNT=`"NT Service\MSOLAP`$$INSTANCENAME`"
ASSYSADMINACCOUNTS=`"$SQLSYSADMINACCOUNTS`"
 
ASDATADIR=`"C:\Program Files\Microsoft SQL Server\MSAS14.SQLSERVER_2017\OLAP\Data`"
ASLOGDIR=`"C:\Program Files\Microsoft SQL Server\MSAS14.SQLSERVER_2017\OLAP\Log`"
ASBACKUPDIR=`"C:\Program Files\Microsoft SQL Server\MSAS14.SQLSERVER_2017\OLAP\Backup`"
ASTEMPDIR=`"C:\Program Files\Microsoft SQL Server\MSAS14.SQLSERVER_2017\OLAP\Temp`"
ASCONFIGDIR=`"C:\Program Files\Microsoft SQL Server\MSAS14.SQLSERVER_2017\OLAP\Config`"
 
ASCOLLATION=`"Latin1_General_CI_AS`"
ASPROVIDERMSOLAP=`"1`"
ASSERVERMODE=`"MULTIDIMENSIONAL`"
BROWSERSVCSTARTUPTYPE=`"Automatic`"
 
 
    "



$tmp = New-TemporaryFile


$execstring = $basestring;
    if($FEATURES.Contains("SQL")) {$execstring=$execstring+$sqlstring}
    if($FEATURES.Contains("AS")) {$execstring=$execstring+$analyticsstring}


$execstring | Out-File $tmp.FullName

return $tmp.FullName

}