########## BEGIN SCRIPT HEADER ########## # DATAPIPE MCD 2012 R2 SQL AUTOMATION - SPIN UP SERVER FROM AMI # Author: Anthony Marino # Version = "1.0.5" # 11/19/2014" ########## END SCRIPT HEADER ########## ##### DO NOT EDIT ##### Param( $Step, $ServerName, $IPAddress, $DNS1, $DNS2, $WinClusterIPList, $WinNodesList, $WSFCName, $AddToCluster ) ##### DO NOT EDIT ##### IF (($Step) -eq "1") { # Remove bullet Remove-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" -Name "Remove" -Force $Message = "STARTING STEP 1." DPLOG -type INFO -Message $Message Write-Host $Message # Create EC2 Automation Status tag and set the value $AWSRegionCode = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").Region) $AWSInstanceID = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").InstanceID) $Time = (Get-Date -Format G) $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "AutomationStatus" $tag.Value = "AUTOMATION STARTED - $Time" New-EC2Tag -Region $AWSRegionCode -ResourceId $AWSInstanceID -Tag $Tag # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force # Write parameters to XML $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") $XML.Variables.Windows."ServerName" = $ServerName $XML.Variables.Windows."IPAddress" = $IPAddress $XML.Variables.Windows."DNS1" = $DNS1 $XML.Variables.Windows."DNS2" = $DNS2 $XML.Save("C:\DPAutomation\Variables.xml") $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") # Setup the server DPRenameAdminToDpadmin DPSetDNSServers -BaseIP $XML.Variables.Windows.IPAddress -DNS1 $XML.Variables.Windows.DNS1 -DNS2 $XML.Variables.Windows.DNS2 DPAWSSetTimezone -Log Y DPRenameServer -ServerName $XML.Variables.Windows.Servername # Remove and set autoresume DPRemoveAutoResumeNoLogon DPAddAutoResumeNoLogon -Command "powershell C:\DPAutomation\DPW2K12R2-NewSQLServer-MCD.ps1 -Step 2" $Message = "STEP 1 COMPLETE. REBOOTING." DPLOG -type INFO -Message $Message Write-Host $Message # Set EC2 Automation Status tag value $AWSRegionCode = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").Region) $AWSInstanceID = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").InstanceID) $Time = (Get-Date -Format G) $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "AutomationStatus" $tag.Value = "STEP 1 COMPLETE - REBOOTING - $Time" New-EC2Tag -Region $AWSRegionCode -ResourceId $AWSInstanceID -Tag $Tag # Reboot Restart-Computer -Force } IF (($Step) -eq "2") { $Message = "STARTING STEP 2." DPLOG -type INFO -Message $Message Write-Host $Message # Set EC2 Automation Status tag value $AWSRegionCode = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").Region) $AWSInstanceID = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").InstanceID) $Time = (Get-Date -Format G) $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "AutomationStatus" $tag.Value = "STEP 2 STARTED - $Time" New-EC2Tag -Region $AWSRegionCode -ResourceId $AWSInstanceID -Tag $Tag # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force # Import XML $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") $DomainName = $XML.Variables.Domain.DomainName $ADUsername = $XML.Variables.Domain.ADUsername $ADPassword = $XML.Variables.Domain.ADPassword $ClientAdminPassword = $XML.Variables.Windows.ClientAdminPass # Create a scheduled task to remove the security banner on reboot and then reboot into step 3 $Password = $ClientAdminPassword | ConvertTo-SecureString -asPlainText -Force $Creds = New-Object System.Management.Automation.PSCredential("ClientAdmin",$Password) Register-ScheduledJob -Name DPRemoveSecurityBanner -FilePath C:\DPAutomation\RemoveBanner.ps1 -Credential $Creds -ScheduledJobOption (New-ScheduledJobOption –DoNotAllowDemandStart) -Trigger (New-JobTrigger –AtStartup -RandomDelay 00:00:30) # Join the domain DPJoinDomain -DomainName $DomainName -ADUsername $ADUsername -ADPassword $ADPassword -Log Y # Remove autoresume DPRemoveAutoResumeNoLogon $Message = "STEP 2 COMPLETE. REBOOTING." DPLOG -type INFO -Message $Message Write-Host $Message # Set EC2 Automation Status tag value $AWSRegionCode = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").Region) $AWSInstanceID = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").InstanceID) $Time = (Get-Date -Format G) $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "AutomationStatus" $tag.Value = "STEP 2 COMPLETE - REBOOTING - $Time" New-EC2Tag -Region $AWSRegionCode -ResourceId $AWSInstanceID -Tag $Tag # Reboot Restart-Computer -Force } IF (($Step) -eq "3") { $Message = "STARTING STEP 3." DPLOG -type INFO -Message $Message Write-Host $Message # Set EC2 Automation Status tag value $AWSRegionCode = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").Region) $AWSInstanceID = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").InstanceID) $Time = (Get-Date -Format G) $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "AutomationStatus" $tag.Value = "STEP 3 STARTED - $Time" New-EC2Tag -Region $AWSRegionCode -ResourceId $AWSInstanceID -Tag $Tag # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force DPConfigDNSSuffix # Remove the scheduled job Unregister-ScheduledJob DPRemoveSecurityBanner # Import XML $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") # Mount point 1 IF (($XML.Variables.MountPoints.MountPoint1."Label") -gt "1") { $Label = $XML.Variables.MountPoints.MountPoint1."Label" $DriveLetter = $XML.Variables.MountPoints.MountPoint1."DriveLetter" $DriveSize = $XML.Variables.MountPoints.MountPoint1."DriveSizeKB" DPAddMountPoint -Label $Label -DriveLetter $DriveLetter -DriveSize $DriveSize } # Mount point 2 IF (($XML.Variables.MountPoints.MountPoint2."Label") -gt "1") { $Label = $XML.Variables.MountPoints.MountPoint2."Label" $DriveLetter = $XML.Variables.MountPoints.MountPoint2."DriveLetter" $DriveSize = $XML.Variables.MountPoints.MountPoint2."DriveSizeKB" DPAddMountPoint -Label $Label -DriveLetter $DriveLetter -DriveSize $DriveSize } # Mount point 3 IF (($XML.Variables.MountPoints.MountPoint3."Label") -gt "1") { $Label = $XML.Variables.MountPoints.MountPoint3."Label" $DriveLetter = $XML.Variables.MountPoints.MountPoint3."DriveLetter" $DriveSize = $XML.Variables.MountPoints.MountPoint3."DriveSizeKB" DPAddMountPoint -Label $Label -DriveLetter $DriveLetter -DriveSize $DriveSize } # Mount point 4 IF (($XML.Variables.MountPoints.MountPoint4."Label") -gt "1") { $Label = $XML.Variables.MountPoints.MountPoint4."Label" $DriveLetter = $XML.Variables.MountPoints.MountPoint4."DriveLetter" $DriveSize = $XML.Variables.MountPoints.MountPoint4."DriveSizeKB" DPAddMountPoint -Label $Label -DriveLetter $DriveLetter -DriveSize $DriveSize } # Mount point 5 IF (($XML.Variables.MountPoints.MountPoint5."Label") -gt "1") { $Label = $XML.Variables.MountPoints.MountPoint5."Label" $DriveLetter = $XML.Variables.MountPoints.MountPoint5."DriveLetter" $DriveSize = $XML.Variables.MountPoints.MountPoint5."DriveSizeKB" DPAddMountPoint -Label $Label -DriveLetter $DriveLetter -DriveSize $DriveSize } # Mount point 6 IF (($XML.Variables.MountPoints.MountPoint6."Label") -gt "1") { $Label = $XML.Variables.MountPoints.MountPoint6."Label" $DriveLetter = $XML.Variables.MountPoints.MountPoint6."DriveLetter" $DriveSize = $XML.Variables.MountPoints.MountPoint6."DriveSizeKB" DPAddMountPoint -Label $Label -DriveLetter $DriveLetter -DriveSize $DriveSize } # Mount point 7 IF (($XML.Variables.MountPoints.MountPoint7."Label") -gt "1") { $Label = $XML.Variables.MountPoints.MountPoint7."Label" $DriveLetter = $XML.Variables.MountPoints.MountPoint7."DriveLetter" $DriveSize = $XML.Variables.MountPoints.MountPoint7."DriveSizeKB" DPAddMountPoint -Label $Label -DriveLetter $DriveLetter -DriveSize $DriveSize } # Mount point 8 IF (($XML.Variables.MountPoints.MountPoint8."Label") -gt "1") { $Label = $XML.Variables.MountPoints.MountPoint8."Label" $DriveLetter = $XML.Variables.MountPoints.MountPoint8."DriveLetter" $DriveSize = $XML.Variables.MountPoints.MountPoint8."DriveSizeKB" DPAddMountPoint -Label $Label -DriveLetter $DriveLetter -DriveSize $DriveSize } # Mount point 9 IF (($XML.Variables.MountPoints.MountPoint9."Label") -gt "1") { $Label = $XML.Variables.MountPoints.MountPoint9."Label" $DriveLetter = $XML.Variables.MountPoints.MountPoint9."DriveLetter" $DriveSize = $XML.Variables.MountPoints.MountPoint9."DriveSizeKB" DPAddMountPoint -Label $Label -DriveLetter $DriveLetter -DriveSize $DriveSize } # Mount point 10 IF (($XML.Variables.MountPoints.MountPoint10."Label") -gt "1") { $Label = $XML.Variables.MountPoints.MountPoint10."Label" $DriveLetter = $XML.Variables.MountPoints.MountPoint10."DriveLetter" $DriveSize = $XML.Variables.MountPoints.MountPoint10."DriveSizeKB" DPAddMountPoint -Label $Label -DriveLetter $DriveLetter -DriveSize $DriveSize } # Continue server setup DPDisableWindowsFW # Remove and set autoresume DPRemoveAutoResumeNoLogon DPAddAutoResumeNoLogon -Command "powershell C:\DPAutomation\DPW2K12R2-NewSQLServer-MCD.ps1 -Step 4" -Username $XML.Variables.Domain.ADUsername -Password $XML.Variables.Domain.ADPassword # Delete security banner Remove-ItemProperty "hklm:\\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" "legalnoticecaption" -Force Remove-ItemProperty "hklm:\\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" "legalnoticetext" -Force $Message = "STEP 3 COMPLETE. REBOOTING." DPLOG -type INFO -Message $Message Write-Host $Message # Set EC2 Automation Status tag value $AWSRegionCode = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").Region) $AWSInstanceID = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").InstanceID) $Time = (Get-Date -Format G) $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "AutomationStatus" $tag.Value = "STEP 3 COMPLETE - REBOOTING - $Time" New-EC2Tag -Region $AWSRegionCode -ResourceId $AWSInstanceID -Tag $Tag Restart-Computer -Force } IF (($Step) -eq "4") { # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force Import-Module C:\DPAutomation\DPSQLAWSPostAMI.psm1 -Force $Message = "STARTING STEP 4." DPLOG -type INFO -Message $Message Write-Host $Message # Set EC2 Automation Status tag value $AWSRegionCode = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").Region) $AWSInstanceID = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").InstanceID) $Time = (Get-Date -Format G) $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "AutomationStatus" $tag.Value = "STEP 4 STARTED - $Time" New-EC2Tag -Region $AWSRegionCode -ResourceId $AWSInstanceID -Tag $Tag # Remove and set autoresume DPRemoveAutoResumeNoLogon # Import XML $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") $isopath = $xml.Variables.SQL.Install.IsoPath DPAddAutoResumeNoLogon -Command "powershell C:\DPAutomation\DPW2K12R2-NewSQLServer-MCD.ps1 -Step 5" -Username $XML.Variables.Domain.ADUsername -Password $XML.Variables.Domain.ADPassword # Setup empheral disks for RAID 0 Init-ephemeralDisks # Setup Node $Message = "STARTING SQL INSTALL." DPLOG -type START -Message $Message Write-Host $Message 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 New-WindowsAdministrator -User $xml.Variables.RedGate.Install.RedGateServiceAccount New-SQLSYSADMIN -User $xml.Variables.RedGate.Install.RedGateServiceAccount InstallRedGate -RedGateServiceAccount $xml.Variables.RedGate.Install.RedGateServiceAccount -RedGateServicePassword $XML.Variables.RedGate.Install.RedGateServicePassword InstallFailoverClusterFeature $Message = "SQL INSTALL COMPLETE." DPLOG -type FINISH -Message $Message Write-Host $Message $Message = "STEP 4 COMPLETE. REBOOTING." DPLOG -type INFO -Message $Message Write-Host $Message # Delete security banner Remove-ItemProperty "hklm:\\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" "legalnoticecaption" -Force Remove-ItemProperty "hklm:\\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" "legalnoticetext" -Force # Set EC2 Automation Status tag value $AWSRegionCode = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").Region) $AWSInstanceID = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").InstanceID) $Time = (Get-Date -Format G) $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "AutomationStatus" $tag.Value = "STEP 4 COMPLETE - REBOOTING - $Time" New-EC2Tag -Region $AWSRegionCode -ResourceId $AWSInstanceID -Tag $Tag Restart-Computer -Force } IF (($Step) -eq "5") { $Message = "STARTING STEP 5." DPLOG -type INFO -Message $Message Write-Host $Message # Set EC2 Automation Status tag value $AWSRegionCode = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").Region) $AWSInstanceID = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").InstanceID) $Time = (Get-Date -Format G) $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "AutomationStatus" $tag.Value = "STEP 5 STARTED - $Time" New-EC2Tag -Region $AWSRegionCode -ResourceId $AWSInstanceID -Tag $Tag # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force Import-Module C:\DPAutomation\DPSQLAWSPostAMI.psm1 -Force # Import XML $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") $AddToCluster = $XML.Variables.Cluster.Install."AddToCluster" [string[]]$NodesList = $XML.Variables.Cluster.Install."NodesList".Split(",") [string[]]$IPList = $XML.Variables.Cluster.Install."IPList".Split(",") # Remove autoresume DPRemoveAutoResumeNoLogon #Setup Cluster if ($AddToCluster = "Yes") { $Message = "STARTING CLUSTER INSTALL." DPLOG -type START -Message $Message Write-Host $Message setupfailoverclusterinstance -Nodes $NodesList -WindowsFciIP $IPList -wsfcname $XML.Variables.Cluster.Install."WSFCName" $Message = "CLUSTER INSTALL COMPLETE." DPLOG -type FINISH -Message $Message Write-Host $Message $Message = "ENABLING ALWAYS ON." DPLOG -type START -Message $Message Write-Host $Message DPEnableSQLAlwaysOnAllNodes -Nodes $NodesList $Message = "ENABLE ALWAYS ON COMPLETE." DPLOG -type FINISH -Message $Message Write-Host $Message } # 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 $Message = "STEP 5 COMPLETE." DPLOG -type INFO -Message $Message Write-Host $Message # Remove the source files Remove-Item C:\DPAutomation\ -Recurse -Force # Set EC2 Automation Status tag value $AWSRegionCode = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").Region) $AWSInstanceID = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").InstanceID) $Time = (Get-Date -Format G) $tag = New-Object Amazon.EC2.Model.Tag $tag.Key = "AutomationStatus" $tag.Value = "AUTOMATION COMPLETE - $Time" New-EC2Tag -Region $AWSRegionCode -ResourceId $AWSInstanceID -Tag $Tag }