<#
Solution: Hydration KIT
Purpose: Install SQL Server 2014 SP1
Version: 1.0 - 2015

Author - Jérôme Bezet-Torres
#>

# Determine where to do the logging 
$tsenv = New-Object -COMObject Microsoft.SMS.TSEnvironment 
$logPath = $tsenv.Value("LogPath") 
$logFile = "$logPath\$($myInvocation.MyCommand).log" 

# Start the logging 
Start-Transcript $logFile 
Write-Host "Logging to $logFile"

$info=get-addomain
$infoC=$info.DNSRoot
$Rdomain=""
$split=$infoC.split(".")
$nb=$split.Count

  switch ($nb)
  {
      2{

        $domain1=$split[0]
        $Ext1=$split[1]
        $Rdomain1= "$domain1.$ext1"
        $UserDomain=$domain1+"\Administrateur"
        }
      3{
        $sdomain=$split[0]
        $domain=$split[1]
        $Ext=$split[2]

        $Rdomain2="$sdomain.$domain.$ext"
        $UserDomain=$domain+"\Administrateur"
    }
  }

$LocalPath = "C:\Scripts\"
$INSTALLSHAREDDIR='INSTALLSHAREDDIR="e:\Program Files\Microsoft SQL Server"'
$SQLSYSADMINACCOUNTS="SQLSYSADMINACCOUNTS=$UserDomain"
$ConfigFile = $LocalPath + "ConfigurationFile.ini"
New-Item -ItemType Directory -Force -Path $LocalPath
Copy-Item "D:\Deploy\Applications\Install - SQL Server 2014\ConfigurationFile.ini" $LocalPath
$configContent =[io.file]::ReadAllText($ConfigFile)
Get-ChildItem $ConfigFile -Recurse |
Where-Object {$_.GetType().ToString() -eq "System.IO.FileInfo"} |
Set-ItemProperty -Name IsReadOnly -Value $false
$configContent = $configContent -Replace 'INSTALLSHAREDDIR="C:\\Program Files\\Microsoft SQL Server"', $INSTALLSHAREDDIR
$configContent = $configContent -Replace 'SQLSYSADMINACCOUNTS="VIAMONSTRA\\Administrator"', $SQLSYSADMINACCOUNTS
$configContent | Out-File $ConfigFile

$exe = "D:\Deploy\Applications\Install - SQL Server 2014\source\setup.exe"
$myargs = "/Configurationfile=$ConfigFile"

# Start Installation
Start-process $exe -ArgumentList $myargs -Wait


Stop-Transcript
