Advertisement
eptesicus

Powershell/PowerCLI - Update VMware Windows template

Aug 25th, 2021
2,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#  
  2.  .SYNOPSIS  
  3.   Script to update Template's Windows Updates
  4.    
  5.  .DESCRIPTION
  6.   Run this script to convert the template to a VM, start the VM,
  7.   apply all Windows Updates, reboot the VM, upgrade VMTools,
  8.   shutdown the VM, note the activity, and convert the VM back
  9.   to a template.
  10.  
  11.  .NOTES  
  12.   Resource: http://woshub.com/automate-update-windows-vmware-template/
  13.   Saving PowerCLI Credentials: https://www.vgemba.net/vmware/powershell/Saving-PowerCLI-Credentials/
  14.  
  15.   The Windows Update Powershell Module must be installed on the VM Template:
  16.   Install-Module -Name PSWindowsUpdate
  17. #>
  18.  
  19.  
  20. #--------------------------------
  21. # Store Credentials:
  22. #--------------------------------
  23. <#
  24.  .Run the below commands to store credentials if updating creds or running for the first time
  25.  
  26.  These must be ran from whatever account the scripts will run from (svc_vmware).
  27.  The scheduled task will also need to be ran from this account.
  28.  
  29.   $VCenterCreds = Get-Credential
  30.   $VCenterCreds | Export-Clixml -Path C:\Scripts\Credentials\VCenterCreds.cred
  31.   $TemplateCreds = Get-Credential
  32.   $TemplateCreds | Export-Clixml -Path C:\Scripts\Credentials\TemplateCreds-Administrator.cred
  33. #>
  34.  
  35. #--------------------------------
  36. # Variables
  37. #--------------------------------
  38.  
  39. $VCenterServer = "MyDomain-VCSA"
  40. $VCenterFQDN = "$VCenterServer.mydomain.net"
  41. $VCenterCreds = Import-Clixml -Path C:\Scripts\Credentials\VCenterCreds.cred    # MyDomain\SVC_VMware
  42. $TemplateName = "tmpWIN2019"
  43. $TemplateCreds = Import-Clixml -Path C:\Scripts\Credentials\TemplateCreds-Administrator.cred    # VM's local administrator
  44. $Date = Get-Date -Format "yyyy-MM-dd"
  45. $LogPath = "C:\Scripts\Logs\$VCenterServer.$TemplateName.Updates.$Date.log"
  46.  
  47.  
  48. #--------------------------------
  49. # Update Process
  50. #--------------------------------
  51.  
  52. # Import the PowerCLI module
  53. Import-Module VMware.VimAutomation.Core -ErrorAction SilentlyContinue
  54.  
  55. #Set to allow working with multiple servers
  56. Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Confirm:$false
  57.  
  58. # Connect to vCenter
  59. Connect-VIServer -Server $VCenterFQDN -Credential $VCenterCreds
  60.  
  61. # Convert a template to a VM
  62. Set-Template -Template $TemplateName -ToVM -Confirm:$false -RunAsync
  63.  
  64. # Make a 15 seconds delay
  65. Start-sleep -s 15 # 15 seconds
  66.  
  67. # Temporary VM notes
  68. $VMnote = "VMAUTO - CURRENTLY UPDATING VM TEMPLATE. DO NOT TOUCH."
  69. Get-VM $TemplateName|Set-VM -Notes $VMnote -Confirm:$false
  70.  
  71. # Start the virtual machine
  72. Start-VM -VM $TemplateName | Get-VMQuestion | Set-VMQuestion -DefaultOption -Confirm:$false
  73. Start-sleep -s 60
  74. Wait-Tools -VM $TemplateName # Wait for VMtools to load
  75. Start-sleep -s 15
  76.  
  77. # Ensure Windows Update Powershell module is installed on VM
  78. Invoke-VMScript -ScriptType PowerShell -ScriptText "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12" -VM $TemplateName -GuestCredential $TemplateCreds | Out-file -Filepath $LogPath -Append
  79. Invoke-VMScript -ScriptType PowerShell -ScriptText "Install-PackageProvider NuGet -Force" -VM $TemplateName -GuestCredential $TemplateCreds | Out-file -Filepath $LogPath -Append
  80. Invoke-VMScript -ScriptType PowerShell -ScriptText "Install-Module -Name PSWindowsUpdate -Force" -VM $TemplateName -GuestCredential $TemplateCreds | Out-file -Filepath $LogPath -Append
  81.  
  82. # Run the command to install all available updates in the guest OS using VMWare Tools (the update installation log is saved to a file)
  83. Invoke-VMScript -ScriptType PowerShell -ScriptText "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot" -VM $TemplateName -GuestCredential $TemplateCreds | Out-file -Filepath $LogPath -Append
  84. Start-sleep -s 30 # 30 seconds
  85.  
  86. # Update VMTools version
  87. Update-Tools -VM $TemplateName -NoReboot
  88.  
  89. # Clean up the WinSxS component store and optimize the image with DISM
  90. Invoke-VMScript -ScriptType PowerShell -ScriptText "Dism.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase" -VM $TemplateName -GuestCredential $TemplateCreds
  91. Start-sleep -s 30 # 30 seconds
  92.  
  93. # Force restart the VM
  94. Restart-VMGuest -VM $TemplateName -Confirm:$false
  95. Start-sleep -s 60
  96. Wait-Tools -VM $TemplateName # Wait for VMtools to load
  97. Start-sleep -s 15
  98.  
  99. # Update VM Notes
  100. $VMnote = "VMAUTO - $((Get-Date -Format "yyyy/MM/dd HH:MM").ToString()) - Updated Windows and VMware tools."
  101. Get-VM $TemplateName|Set-VM -Notes $VMnote -Confirm:$false
  102.  
  103. # Shut the VM down and convert it back to the template
  104. Shutdown-VMGuest -VM $TemplateName -Confirm:$false
  105. do { $vmstatus = (get-vm $TemplateName).PowerState ; Start-Sleep -Seconds 5} while ($vmstatus -ne "PoweredOff") # WAIT UNTIL POWERED OFF
  106. Set-VM -VM $TemplateName -ToTemplate -Confirm:$false
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement