########## BEGIN SCRIPT HEADER ########## # DATAPIPE MCD 2012 R2 SQL AUTOMATION - PREPARE AMI # Author: Anthony Marino # Version = "1.0.2" # 10/24/2014" ########## END SCRIPT HEADER ########## ##### DO NOT EDIT ##### Param( $Step, $ClientAdminPass, $DomainName, $ADUsername, $ADPassword, $EmailTo, $DisableBullet ) ##### DO NOT EDIT ##### IF (($Step) -eq "1") { # Check if the OS is supported IF (((Get-WmiObject Win32_OperatingSystem).Version) -lt "6.2.") { # Unsupported OS $Message = "THIS OS IS NOT SUPPORTED BY THIS FUNCTION" DPLOG -type ERROR -Message $Message Write-Host $Message Exit } # Create the working directory New-Item -Type Directory 'C:\DPAutomation\' -Force # Download source files Invoke-WebRequest 'https://toolkit.datapipe.net/BesTasks/anthony/McDonalds/PSM/DPWindowsPSM-MCD.psm1' -OutFile 'C:\DPAutomation\DPWindowsPSM-MCD.psm1' Invoke-WebRequest 'https://toolkit.datapipe.net/BesTasks/anthony/McDonalds/AWSAutomation/W2K12R2_SQL_Setup/DPW2K12R2-NewSQLServer-MCD.ps1' -OutFile 'C:\DPAutomation\DPW2K12R2-NewSQLServer-MCD.ps1' Invoke-WebRequest 'https://toolkit.datapipe.net/BesTasks/anthony/McDonalds/AWSAutomation/W2K12R2_SQL_Setup/Variables.xml' -OutFile 'C:\DPAutomation\Variables.xml' Invoke-WebRequest 'https://toolkit.datapipe.net/bestasks/postinstalls/mcd/SecurityControlInstalls-MCD.ps1' -OutFile 'C:\DPAutomation\SecurityControlInstalls-MCD.ps1' IF (!(($DisableBullet) -eq "Y")) { Invoke-WebRequest 'https://toolkit.datapipe.net/BesTasks/anthony/McDonalds/AWSAutomation/W2K12R2_SQL_Setup/Bullet.ps1' -OutFile 'C:\DPAutomation\Bullet.ps1' } # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force # Prompt for input function DPForm ([string]$Message, [string]$WindowTitle, [string]$DefaultText) { Add-Type -AssemblyName Microsoft.VisualBasic return [Microsoft.VisualBasic.Interaction]::InputBox($Message, $WindowTitle, $DefaultText) } $ClientAdminPass = DPForm -Message "CLIENTADMIN PASSWORD:" -WindowTitle "DP - MCD - PREPARE AMI" $DomainName = DPForm -Message "DOMAIN NAME:" -WindowTitle "DP - MCD - PREPARE AMI" $ADUsername = DPForm -Message "DOMAIN USERNAME:" -WindowTitle "DP - MCD - PREPARE AMI" $ADPassword = DPForm -Message "DOMAIN PASSWORD:" -WindowTitle "DP - MCD - PREPARE AMI" $EmailTo = DPForm -Message "TO EMAIL ADDRESS::" -WindowTitle "DP - MCD - PREPARE AMI" # Write input to XML $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") $XML.Variables.Windows."ClientAdminPass" = $ClientAdminPass $XML.Variables.Domain."DomainName" = $DomainName $XML.Variables.Domain."ADUsername" = $ADUsername $XML.Variables.Domain."ADPassword" = $ADPassword $XML.Variables.General."EmailTo" = $EmailTo IF (($DisableBullet) -eq "Y") { $XML.Variables.General."DisableBullet" = $DisableBullet } $XML.Save("C:\DPAutomation\Variables.xml") # Send start email $InstanceID = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").InstanceID) DPSendEmail -To $XML.Variables.General.EmailTo -Subject "MCD SQL - PREPARE AMI - STARTED - INSTANCE: $InstanceID" -Body "This job has started. Depending on the number of Windows Updates the process can take between 15 and 30 minutes to complete. You will receive another email upon completion. Instance ID: $InstanceID" # Apply Windows customizations DPInstallNET35 DPAddLocalUser -Username clientadmin -Password $XML.Variables.Windows.ClientAdminPass DPDisableWindowsFW DPAWSSetTimezone -Log Y DPEnableMultipleRDP DPInstallWindowsUpdates -InstallOptional 'Y' # Add autoresume DPAddAutoResumeNoLogon -Command "powershell C:\DPAutomation\DPW2K12R2-NewSQLServer-MCD-PrepareAMI.ps1 -Step 2" # Reboot $Message = "REBOOTING." DPLOG -type INFO -Message $Message Write-Host $Message Restart-Computer -Force } IF (($Step) -eq "2") { # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") $Message = "STARTING SECURITY CONTROL INSTALLATIONS." DPLOG -type INFO -Message $Message Write-Host $Message # Harden the server Invoke-Expression 'C:\DPAutomation\SecurityControlInstalls-MCD.ps1 -ForImaging' $Message = "EXPORTING LEGAL NOTICE MESSAGES AND REMOVING THEM." DPLOG -type INFO -Message $Message Write-Host $Message ((Get-ItemProperty -Path 'hklm:\\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system').legalnoticecaption) | Out-File C:\DPAutomation\legalnoticetext.txt ((Get-ItemProperty -Path 'hklm:\\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system').legalnoticetext) | Out-File C:\DPAutomation\legalnoticetext.txt Remove-ItemProperty "hklm:\\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" "legalnoticecaption" -Force Remove-ItemProperty "hklm:\\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\system" "legalnoticetext" -Force # Remove Autoresume DPRemoveAutoResumeNoLogon # Add autoresume DPAddAutoResumeNoLogon -Command "powershell C:\DPAutomation\DPW2K12R2-NewSQLServer-MCD-PrepareAMI.ps1 -Step 3" $InstanceID = ((Invoke-RestMethod "http://169.254.169.254/latest/dynamic/instance-identity/document").InstanceID) # Send completion email DPSendEmail -To $XML.Variables.General.EmailTo -Subject "MCD SQL - PREPARE AMI - COMPLETED - INSTANCE: $InstanceID" -Body "This job is almost complete. Due to limitations for MCD's antivirus, you will receive this message prior to the last reboot. Please allow 3 minutes before connecting. Attached is the log file up to this point. Instance ID: $InstanceID" -Attachment C:\Windows\Temp\DPAutomation.log # Reboot $Message = "REBOOTING." DPLOG -type INFO -Message $Message Write-Host $Message Restart-Computer -Force } IF (($Step) -eq "3") { # Import the Windows PSM Import-Module C:\DPAutomation\DPWindowsPSM-MCD.psm1 -Force $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") # Remove autoresume DPRemoveAutoResumeNoLogon ########## BEGIN OPTIONS ########## $RemoveMcAfeeKeys = "Y" $RemoveBESKeys = "Y" ########## END OPTIONS ########## ########## BEGIN SCRIPT ########## ########## BEGIN REMOVE MCAFEE KEYS FUNCTION ########## IF (($RemoveMcAfeeKeys) -eq "Y") { $Message = "OPTION TO DELETE THE MCAFEE KEYS DETECTED AS ENABLED." DPLOG -type INFO -Message $Message Write-Host $Message # If the keys exist, delete them IF ((Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent").AgentGUID) { # McAfee Agent GUID key detected $Message = "MCAFEE AGENT GUID KEY DETECTED. DELETING IT." DPLOG -type INFO -Message $Message Write-Host $Message Remove-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent" -name AgentGUID IF (!(Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent").AgentGUID) { # Validated $Message = "CONFIRMED THE MCAFEE AGENT GUIS KEY WAS DELETED." DPLOG -type SUCCESS -Message $Message Write-Host $Message } ELSE { $Message = "UNABLE TO DELETE THE MCAFEE AGENT GUID KEY." DPLOG -type ERROR -Message $Message Write-Host $Message } } ELSE { $Message = "MCAFEE AGENT GUID KEY WAS NOT DETECTED." DPLOG -type INFO -Message $Message Write-Host $Message } IF ((Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent").MacAddress) { # McAfee Agent MAC address key detected $Message = "MCAFEE MAC ADDRESS KEY DETECTED. DELETING IT." DPLOG -type INFO -Message $Message Write-Host $Message Remove-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent" -name MacAddress IF (!(Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Network Associates\ePolicy Orchestrator\Agent").MacAddress) { # Validated $Message = "CONFIRMED THE MCAFEE MAC ADDRESS KEY WAS DELETED." DPLOG -type SUCCESS -Message $Message Write-Host $Message } ELSE { $Message = "UNABLE TO DELETE THE MCAFEE MAC ADDRESS KEY." DPLOG -type ERROR -Message $Message Write-Host $Message } } ELSE { $Message = "MCAFEE MAC ADDRESS KEY WAS NOT DETECTED." DPLOG -type INFO -Message $Message Write-Host $Message } } ELSE { $Message = "OPTION TO DELETE THE MCAFEE KEYS DETECTED AS DISABLED." DPLOG -type INFO -Message $Message Write-Host $Message } ########## END REMOVE MCAFEE KEYS FUNCTION ########## ##################################################################################### ##################################################################################### ########## BEGIN REMOVE BES KEYS AND DIRECTORY FUNCTION ########## IF (($RemoveBESKeys) -eq "Y") { $Message = "OPTION TO DELETE THE BES KEYS DETECTED AS ENABLED." DPLOG -type INFO -Message $Message Write-Host $Message # Check if BES is installed IF (Get-Service "BESClient") { $Message = "BES SERVICE DETECTED. DOWNLOADING AND RUNNING THE BES UNLOCKER." DPLOG -type INFO -Message $Message Write-Host $Message Invoke-WebRequest 'http://toolkit.datapipe.net/security/bes/unlock.exe' -OutFile 'C:\DPAutomation\unlock.exe' Start-Process C:\DPAutomation\unlock.exe $Message = "BES SERVICE UNLOCKED." DPLOG -type SUCCESS -Message $Message Write-Host $Message # Stop BES service $Message = "STOPPING THE BES SERVICE." DPLOG -type INFO -Message $Message Write-Host $Message Stop-Service "BESClient" # Validate IF ((Get-Service "BESClient").Status -eq "Stopped") { $Message = "CONFIRMED THE BES SERVICE WAS STOPPED." DPLOG -type SUCCESS -Message $Message Write-Host $Message # Continue # If the keys exist, delete them IF ((Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\BigFix\EnterpriseClient\GlobalOptions").RegCount) { # BES RegCount Key detected $Message = "BES REGCOUNT KEY DETECTED. DELETING IT." DPLOG -type INFO -Message $Message Write-Host $Message Remove-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\BigFix\EnterpriseClient\GlobalOptions" -name RegCount IF (!(Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\BigFix\EnterpriseClient\GlobalOptions").RegCount) { # Validated $Message = "CONFIRMED THE BES REGCOUNT KEY WAS DELETED." DPLOG -type SUCCESS -Message $Message Write-Host $Message } ELSE { $Message = "UNABLE TO DELETE THE BES REGCOUNT KEY." DPLOG -type ERROR -Message $Message Write-Host $Message } } ELSE { $Message = "BES REGCOUNT KEY WAS NOT DETECTED." DPLOG -type INFO -Message $Message Write-Host $Message } IF ((Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\BigFix\EnterpriseClient\GlobalOptions").ReportSequenceNumber) { # BES ReportSequenceNumber Key detected $Message = "BES REPORTSEQUENCENUMBER KEY DETECTED. DELETING IT." DPLOG -type INFO -Message $Message Write-Host $Message Remove-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\BigFix\EnterpriseClient\GlobalOptions" -name ReportSequenceNumber IF (!(Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\BigFix\EnterpriseClient\GlobalOptions").ReportSequenceNumber) { # Validated $Message = "CONFIRMED THE BES REPORTSEQUENCENUMBER KEY WAS DELETED." DPLOG -type SUCCESS -Message $Message Write-Host $Message } ELSE { $Message = "UNABLE TO DELETE THE BES REPORTSEQUENCENUMBER KEY." DPLOG -type ERROR -Message $Message Write-Host $Message } } ELSE { $Message = "BES REGCOUNT KEY WAS NOT DETECTED." DPLOG -type INFO -Message $Message Write-Host $Message } IF ((Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\BigFix\EnterpriseClient\GlobalOptions").ComputerID) { # BES ComputerID Key detected $Message = "BES COMPUTERID KEY DETECTED. DELETING IT." DPLOG -type INFO -Message $Message Write-Host $Message Remove-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\BigFix\EnterpriseClient\GlobalOptions" -name ComputerID IF (!(Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\BigFix\EnterpriseClient\GlobalOptions").ComputerID) { # Validated $Message = "CONFIRMED THE BES COMPUTERID KEY WAS DELETED." DPLOG -type SUCCESS -Message $Message Write-Host $Message } ELSE { $Message = "UNABLE TO DELETE THE BES COMPUTERID KEY." DPLOG -type ERROR -Message $Message Write-Host $Message } } ELSE { $Message = "BES COMPUTERID KEY WAS NOT DETECTED." DPLOG -type INFO -Message $Message Write-Host $Message } # Delete the TEM install folder IF (Test-Path "C:\Program Files\BigFix Enterprise\BES Client") { $Message = "TEM INSTALL FOLDER DETECTED. DELETING IT." DPLOG -type INFO -Message $Message Write-Host $Message Remove-Item "C:\Program Files\BigFix Enterprise\BES Client" IF (!(Test-Path "C:\Program Files\BigFix Enterprise\BES Client")) { $Message = "CONFIRMED THE TEM INSTALL FOLDER WAS DELETED." DPLOG -type SUCCESS -Message $Message Write-Host $Message } ELSE { $Message = "UNABLE TO DELETE THE TEM INSTALL FOLDER." DPLOG -type ERROR -Message $Message Write-Host $Message } } ELSE { $Message = "TEM INSTALL FOLDER NOT DETECTED." DPLOG -type INFO -Message $Message Write-Host $Message } # Lock BES $Message = "DOWNLOADING THE BES LOCK TOOL AND RUNNING IT." DPLOG -type INFO -Message $Message Write-Host $Message Invoke-WebRequest 'http://toolkit.datapipe.net/security/bes/lock.exe' -OutFile 'C:\DPAutomation\lock.exe' Start-Process C:\DPAutomation\lock.exe } ELSE { $Message = "BES WAS UNABLE TO BE STOPPED." DPLOG -type ERROR -Message $Message Write-Host $Message } } ELSE { $Message = "THE BES SERVICE IS NOT INSTALLED." DPLOG -type ERROR -Message $Message Write-Host $Message } } ########## END REMOVE BES KEYS AND DIRECTORY FUNCTION ########## ##################################################################################### ##################################################################################### $XML = [XML](Get-Content "C:\DPAutomation\Variables.xml") IF (!(($XML.Variables.General.DisableBullet) -eq "Y")) { # Allow 1 login $command = "powershell -windowstyle minimized C:\DPAutomation\Bullet.ps1" Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" -Name "Remove" -Type String -Value $Command } $Message = "COMPLETE. YOUR AMI IS NOW READY TO BE SHUTDOWN WITH OR WITHOUT SYSPREP." DPLOG -type FINISH -Message $Message Write-Host $Message }