########## BEGIN SCRIPT HEADER ########## # DATAPIPE MCD 2012 R2 SQL AUTOMATION - SPIN UP SERVER FROM AMI # Author: Anthony Marino # Version = "1.0.3" # 10/27/2014" ########## END SCRIPT HEADER ########## ##### DO NOT EDIT ##### Param( $Step, $ServerName, $IPAddress, $DNS1, $DNS2, $WinClusterIPList, $WinNodesList, $WSFCName, $AddToCluster ) ##### DO NOT EDIT ##### IF (($Step) -eq "1") { $Message = ("Step 1 started") DPLOG -type INFO -Message $Message Write-Host $Message # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force Invoke-WebRequest -Uri "https://toolkit.datapipe.net/bestasks/justin/dpsqlawspostami.psm1" -OutFile "C:\DPAutomation\dpsqlawspostami.psm1" DPAddAutoResumeNoLogon -Command "powershell C:\DPAutomation\DPW2K12R2-NewSQLServer-MCD.ps1 -Step 2" # Reboot $Message = ("Step 1 finished") DPLOG -type INFO -Message $Message Write-Host $Message Restart-Computer -Force } IF (($Step) -eq "2") { $Message = ("Step 2 started") DPLOG -type INFO -Message $Message Write-Host $Message # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force Invoke-WebRequest -Uri "https://toolkit.datapipe.net/bestasks/justin/dpsqlawspostami.psm1" -OutFile "C:\DPAutomation\dpsqlawspostami.psm1" DPRemoveAutoResumeNoLogon DPAddAutoResumeNoLogon -Command "powershell C:\DPAutomation\DPW2K12R2-NewSQLServer-MCD.ps1 -Step 3" # Reboot $Message = ("Step 2 finished") DPLOG -type INFO -Message $Message Write-Host $Message Restart-Computer -Force } IF (($Step) -eq "3") { $Message = ("Step 3 started") DPLOG -type INFO -Message $Message Write-Host $Message # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force Invoke-WebRequest -Uri "https://toolkit.datapipe.net/bestasks/justin/dpsqlawspostami.psm1" -OutFile "C:\DPAutomation\dpsqlawspostami.psm1" DPRemoveAutoResumeNoLogon DPAddAutoResumeNoLogon -Command "powershell C:\DPAutomation\DPW2K12R2-NewSQLServer-MCD.ps1 -Step 4" # Reboot $Message = ("Step 3 finished") DPLOG -type INFO -Message $Message Write-Host $Message Restart-Computer -Force } IF (($Step) -eq "4") { # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force $Message = ("Step 4 started") DPLOG -type INFO -Message $Message Write-Host $Message # Import XML $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") # Join the domain $DomainName = $XML.Variables.Domain.DomainName $ADUsername = $XML.Variables.Domain.ADUsername $ADPassword = $XML.Variables.Domain.ADPassword DPJoinDomain -DomainName $DomainName -ADUsername $ADUsername -ADPassword $ADPassword -Log Y # Remove and set autoresume DPRemoveAutoResumeNoLogon try{ DPAddAutoResumeNoLogon -Command "powershell C:\DPAutomation\DPW2K12R2-NewSQLServer-MCD.ps1 -Step 5" $Message = ("Step 4 finished") DPLOG -type INFO -Message $Message Write-Host $Message # Reboot Restart-Computer -Force } catch [System.Exception] { $Message = ("Error was recorded .") DPLOG -type INFO -Message $Message Write-Host $Message } } IF (($Step) -eq "5") { # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force $Message = ("Step 3 started") DPLOG -type INFO -Message $Message Write-Host $Message # Import XML $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") $ADUsername = $XML.Variables.Domain.ADUsername $ADPassword = $XML.Variables.Domain.ADPassword # Continue server setup DPDisableWindowsFW # Remove and set autoresume DPRemoveAutoResumeNoLogon DPAddAutoResumeNoLogon -Command "powershell C:\DPAutomation\DPW2K12R2-NewSQLServer-MCD.ps1 -Step 6" -Username $ADUsername -Password $ADPassword Restart-Computer -Force } IF (($Step) -eq "6") { # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force Import-Module C:\DPAutomation\DPSQLAWSPostAMI.psm1 -Force # Remove and set autoresume DPRemoveAutoResumeNoLogon # Import XML $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") $isopath = $xml.Variables.SQL.Install.IsoPath $ADUsername = $XML.Variables.Domain.ADUsername $ADPassword = $XML.Variables.Domain.ADPassword $RedGateServiceAccount = "mcdtest.local\SQLRGBACKUP001" $RedGateServicePassword = 'KmgD45gtaADG2' $Message = ("Starting SQL as user {0}\{1}." -f [environment]::domain,[environment]::user) DPLOG -type INFO -Message $Message Write-Host $Message DPAddAutoResumeNoLogon -Command "powershell C:\DPAutomation\DPW2K12R2-NewSQLServer-MCD.ps1 -Step 7" -Username $ADUsername -Password $ADPassword #Setup Node Mount-DiskImage -ImagePath $isoPath; $MountLetter = (Get-DiskImage -ImagePath $isoPath | Get-Volume).driveletter $cmd = $MountLetter + ":\setup.exe" & $cmd /configurationfile="c:\DPAutomation\SQLInstallConfiguration.ini" /IACCEPTSQLSERVERLICENSETERMS Dismount-DiskImage -ImagePath $isoPath $Message = ("Finished SQL as user {0}\{1}." -f [environment]::domain,[environment]::user) DPLOG -type INFO -Message $Message Write-Host $Message $Message = ("Start new-windowssysadmin and sqlsysadmin to add user {0}." -f $redgateserviceaccount) DPLOG -type INFO -Message $Message Write-Host $Message New-WindowsAdministrator -User $RedGateServiceAccount New-SQLSYSADMIN -User $RedGateServiceAccount $Message = ("Finish new-windowssysadmin and sqlsysadmin to add user {0}." -f $redgateserviceaccount) DPLOG -type INFO -Message $Message Write-Host $Message $Message = ("Start installredgate with user {0}." -f $redgateserviceaccount) DPLOG -type INFO -Message $Message Write-Host $Message InstallRedGate -RedGateServiceAccount $RedGateServiceAccount -RedGateServicePassword $RedGateServicePassword $Message = ("Finish installredgate with user {0}." -f $redgateserviceaccount) DPLOG -type INFO -Message $Message Write-Host $Message InstallFailoverClusterFeature #Setup Cluster if ($AddToCluster = "Yes") { setupfailoverclusterinstance -Nodes $WinNodesList -WindowsFciIP $WinClusterIPList -WindowsFciName $WSFCName } Restart-Computer -Force } IF (($Step) -eq "7") { # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force Import-Module C:\DPAutomation\DPSQLAWSPostAMI.psm1 -Force # Remove autoresume DPRemoveAutoResumeNoLogon if ($AddToCluster = "Yes") { import-module sqlps DPEnableSQLAlwaysOnAllNodes -Nodes $WinNodesList } # Add security legal notice $Message = "IMPORTING AND ENABLING LEGAL NOTICE MESSAGES." DPLOG -type INFO -Message $Message Write-Host $Message $legalnoticecaption = Get-Content C:\DPAutomation\legalnoticecaption.txt $legalnoticetext = Get-Content C:\DPAutomation\legalnoticetext.txt Set-ItemProperty "hklm:\\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" -Name legalnoticecaption -Value $legalnoticecaption Set-ItemProperty "hklm:\\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" -Name legalnoticetext -Value $legalnoticetext }