Để triển khai máy ảo mới trong VMWare, VM template thường được sử dụng. VM template VMWare là bản sao máy ảo tham chiếu với cài đặt đã cấu hình, phần mềm đã cài đặt và bản cập nhật bảo mật. Quản trị viên cần thường xuyên cập nhật VM template để giữ nó cập nhật: cài đặt bản cập nhật bảo mật Windows mới (ít nhất một lần một tháng), cập nhật ứng dụng hệ thống và ứng dụng, cập nhật định nghĩa antivirus, v.v.
Quy trình cập nhật VM template trên VMWare bao gồm các giai đoạn sau:
- Template từ Content Library được chuyển đổi thành máy ảo;
- Sau khi khởi động, quản trị viên đăng nhập, cài đặt bản cập nhật Windows đã phê duyệt qua WSUS, cập nhật phần mềm cần thiết;
- Sau khi cài đặt bản cập nhật, VM được khởi động lại, sau đó tắt và chuyển đổi lại thành template.
Trong bài viết này, chúng tôi sẽ chỉ cách cài đặt bản cập nhật Windows vào VM template VMWare tự động mà không cần làm gì thủ công.
Đối với máy ảo VMWare, bạn sẽ không thể trực tiếp dùng các phương pháp tích hợp bản cập nhật vào hình ảnh Windows dùng cho hình ảnh độc lập hoặc distro.
Bạn có thể dùng PowerCLI để cài đặt bản cập nhật vào máy ảo Windows. Chúng ta giả định VMWare tools, PowerShell phiên bản 4 (hoặc mới hơn), và module PSWindowsUpdate được cài đặt trong VM template. Chạy script PowerShell phải được phép trong hệ điều hành khách bởi chính sách thực thi script.
Script PowerCLI dưới đây sẽ giúp bạn tự động chuyển đổi VMWare template thành VM và cài đặt bản cập nhật bảo mật từ WSUS:
# Import the PowerCLI module
Import-Module VMware.VimAutomation.Core -ErrorAction SilentlyContinue
# Connect to vCenter
connect-viserver de-vcenter1
$TeplateVMName="Win2016StdTemplate"
# Convert a template to a VM
Set-Template -Template $TeplateVMName -ToVM -Confirm:$false –RunAsync
# Make a 60 seconds delay
Start-sleep -s 60
# Start the virtual machine
Start-VM -VM $TeplateVMName | Get-VMQuestion | Set-VMQuestion -DefaultOption -Confirm:$false
Start-sleep -s 120
# Get an administrator credentials from an encrypted file (if you do not want to keep the password in the PS script in clear text)
$adminname = "administrator"
$Pwd = Get-Content c:\Scripts\VMWare\vm_admin_passfile.txt | ConvertTo-SecureString $
cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $adminname, $Pwd
# Run the command to install all available updates in the guest OS using VMWare Tools (the update installation log is saved to a file: C:\temp\Update.log)
Giả định VM nằm trong mạng ảo nơi nó có thể lấy địa chỉ IP từ server DHCP. Cài đặt WSUS trong máy ảo được lưu trong registry thay vì triển khai qua GPO.
Invoke-VMScript -ScriptType PowerShell -ScriptText "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot" -VM $TeplateVMName -GuestCredential $Cred | Out-file -Filepath C:\temp\Update.log -Append
Start-sleep -s 1800
# Update VMTools version
Update-Tools -VM $TeplateVMName -NoReboot
# Clean up the WinSxS component store and optimize the image with DISM
Invoke-VMScript -ScriptType PowerShell -ScriptText "Dism.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase" -VM $TeplateVMName -GuestCredential $Cred
Start-sleep -s 1800
# Force restart the VM
Restart-VMGuest -VM $TeplateVMName -Confirm:$false
# Shut the VM down and convert it back to the template
Shutdown-VMGuest –VM $TeplateVMName -Confirm:$false –RunAsync
Start-sleep -s 180
Set-VM –VM $TeplateVMName -ToTemplate -Confirm:$false
Bạn có thể thêm script PowerShell này vào Task Scheduler để tự động cài đặt bản cập nhật vào template một lần một tháng vài ngày sau Microsoft Patch Tuesday. Sau đó, nếu bạn triển khai máy ảo mới từ VMWare template, bạn có thể chắc chắn rằng bản cập nhật bảo mật Microsoft mới nhất đã được cài đặt trong nó.