<#
Solution: Hydration SCCM 1511
Purpose: Install Hydration SCCM 1511
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
function get-UPN ($param1)
{
$Rdomain=""
$split=$param1.split(".")
$nb=$split.Count

  switch ($nb)
  {
      2{

        $domain1=$split[0]
        $Ext1=$split[1]
        $Rdomain= "$domain1.$ext1"

        return $Rdomain}
      3{
        $sdomain=$split[0]
        $domain=$split[1]
        $Ext=$split[2]

        $Rdomain="$sdomain.$domain.$ext"

    return $Rdomain}
  }
}

$dom = get-UPN $infoC

#Variables
$LocalPath = "C:\Scripts\"
$SDKServer="SDKServer=$env:computername.$dom"
$ManagementPoint="ManagementPoint=$env:computername.$dom"
$DistributionPoint="ManagementPoint=$env:computername.$dom"
$SQLServerName="SQLServerName=$env:computername.$dom"
$CloudConnectorServer="CloudConnectorServer=$env:computername.$dom"
$ConfigFile = $LocalPath + "ConfigMgrUnattend.ini"

New-Item -ItemType Directory -Force -Path $LocalPath
Copy-Item "D:\Deploy\Applications\Install - ConfigMgr\ConfigMgrUnattend.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 'SDKServer=CM01.corp.viamonstra.com', $SDKServer
$configContent = $configContent -Replace 'ManagementPoint=CM01.corp.viamonstra.com', $ManagementPoint
$configContent = $configContent -Replace 'DistributionPoint=CM01.corp.viamonstra.com', $DistributionPoint
$configContent = $configContent -Replace 'SQLServerName=CM01.corp.viamonstra.com', $SQLServerName
$configContent = $configContent -Replace 'CloudConnectorServer=CM01.corp.viamonstra.com', $CloudConnectorServer
$configContent | Out-File $ConfigFile

# Workaround for bug in ConfigMgr / MDT 2013 Update 1, where ConfigMgr setup deletes the registry info the MDT task sequence needs
 reg export HKLM\SOFTWARE\Microsoft\SMS C:\Windows\Temp\TS.reg

$exe = "D:\Deploy\Applications\Install - ConfigMgr\source\SMSSETUP\BIN\X64\setup.exe"
$myargs = "/Script $ConfigFile /NoUserInput"

# Start Installation
Write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" -ForegroundColor Green
Write-Host "             Install - ConfigMgr 1511                " -ForegroundColor Green
Write-Host "                   Be patient...                     " -ForegroundColor Green
Write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" -ForegroundColor Green

Start-process $exe -ArgumentList $myargs -Wait

Write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" -ForegroundColor Green
Write-Host "             Install - ConfigMgr 1511"                 -ForegroundColor Green
Write-Host "                      Finished                       " -ForegroundColor Green
Write-Host "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" -ForegroundColor Green

Start-Sleep -Seconds 1

# Workaround for bug in ConfigMgr / MDT 2013 Update 1, where ConfigMgr setup deletes the registry info the MDT task sequence needs
$cmd= "regedit /s C:\Windows\Temp\TS.reg"
Invoke-Expression $cmd | Out-Null



Stop-Transcript
