当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PowerShell 更新 VMWare 上的 Windows VM 模板

作者:精品下载站 日期:2024-12-15 00:44:39 浏览:13 分类:玩电脑

使用 PowerShell 更新 VMWare 上的 Windows VM 模板


要在 VMWare 中部署新虚拟机,通常使用 VM 模板。 VMWare VM 模板是一个参考虚拟机副本,其中包含已配置的设置、已安装的软件和安全更新。管理员需要定期更新虚拟机模板以使其保持最新状态:安装新的 Windows 安全更新(至少每月一次)、更新系统和应用程序应用程序、更新防病毒定义等。

VMWare 上虚拟机模板的更新过程包括以下阶段:

  1. 内容库中的模板将转换为虚拟机。

  2. 启动后,管理员登录,使用 WSUS 安装批准的 Windows 更新,更新所需的软件;

  3. 安装更新后,VM 将重新启动,然后关闭并转换回模板。

在本文中,我们将展示如何自动将 Windows 更新安装到 VMWare 虚拟机模板,而无需手动执行任何操作。

对于 VMWare 虚拟机,您将无法直接使用用于独立映像或发行版的将更新集成到 Windows 映像的方法。

您可以使用 PowerCLI 将更新安装到 Windows 虚拟机。我们假设 VMWare 工具、PowerShell 版本 4(或更高版本)和 PSWindowsUpdate 模块安装在虚拟机模板中。脚本执行策略必须允许在来宾操作系统中运行 PowerShell 脚本。

下面的 PowerCLI 脚本将帮助您自动将 VMWare 模板转换为 VM 并从 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)

假设 VM 位于虚拟网络中,可以从 DHCP 服务器获取 IP 地址。虚拟机中的 WSUS 设置存储在注册表中,而不是通过 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

[玩转系统] 使用 PowerShell 更新 VMWare 上的 Windows VM 模板

您可以将此 PowerShell 脚本添加到任务计划程序,以便每月在 Microsoft 补丁星期二后几天自动安装模板更新。然后,如果您从 VMWare 模板部署新虚拟机,则可以确保其中安装了最新的 Microsoft 安全更新。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯