Holy cow it’s been a while! Real life must be catching up with me or something, I am not sure. Anywho, I’ve had this issue where I have never felt comfortable automating Windows Updates on my servers in case the worst should happen. Seems like a reasonable concern right? Well I finally got over it, did some digging, and found a suitable solution that I am comfortable with.
It really is quite simple. I hijacked some PowerShell modules from Technet Script Center (found here) and then did some work with the PowerCLI module from VMware to take a snapshot prior to the updates. Once it is done taking a snapshot of a VM (I pull them from an array) it will begin updates, and restart as needed. I do not delete the snapshots in an automated fashion at this time, I just check everything is still good when I get in the office in the morning and then remove them all with PowerCLI (assuming there is no issues) as seen below:
1 |
Get-Snapshot -VM * -Name * | Remove-Snapshot |
Since I said I run this while I’m sleeping, I obviously just have it set up with Task Scheduler running with a service account I created with access to all the necessary servers and my vCenter environment. Without further ado, I give you what I have coined “UpdateAutoPilot” below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<# .SYNOPSIS This script will create a snapshot of each VM in array and then begin the windows update process. .DESCRIPTION This uses both PowerCLI and PSWindowsUpdate modules found at https://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc/. .NOTES Author: Scott Shelton #> $dtMD = Get-Date -Format M $strSMTPServer = "SMTP Relay" $arr_vCenter_VMs = Get-Content C:\Automated\Arrays\arr_vCenter_VMs.txt $objWUScript = {ipmo PSWindowsUpdate; Get-WUInstall -AcceptAll -AutoReboot| Out-File C:\Automated\Logs\PSWindowsUpdate.log} Send-MailMessage -SmtpServer $strSMTPServer -To "Sysadmin <sysadmin@domain.com>" -From "PowerShell Automation <automated@domain.com>" -Subject "UpdateAutoPilot-vCenter Has Begun" -Body "The automated Windows Update script UpdateAutoPilot-vCenter.ps1 located at \\Location has begun." Import-Module VMware.PowerCLI Connect-VIServer -Server vCENTER foreach($tmpVM_Name in $arr_vCenter_VMs) { $dtMDYT = Get-Date -Format G Get-VM $tmpVM_Name | New-Snapshot -Name "$tmpVM_Name $dtMD" -Description "$dtMDYT - Snapshot prior to Windows Updates Installation via UpdateAutoPilot-vCenter.ps1" -Memory:$true -Confirm:$false Invoke-WUInstall -ComputerName $tmpVM_Name -Script $objWUScript -Confirm:$false } Send-MailMessage -SmtpServer $strSMTPServer -To "Sysadmin <sysadmin@domain.com>" -From "PowerShell Automation <automated@domain.com>" -Subject "UpdateAutoPilot-vCenter Has Completed" -Body "The automated Windows Update script UpdateAutoPilot-vCenter.ps1 located at \\Location has completed. Though the script has completed, virtual machines may still be installing updates. See log on VM at C:\Automated\Logs\PSWindowsUpdate.log for more information. REMINDER: Snapshots still need to be removed." |