Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Script to update Template's Windows Updates
- .DESCRIPTION
- Run this script to convert the template to a VM, start the VM,
- apply all Windows Updates, reboot the VM, upgrade VMTools,
- shutdown the VM, note the activity, and convert the VM back
- to a template.
- .NOTES
- Resource: http://woshub.com/automate-update-windows-vmware-template/
- Saving PowerCLI Credentials: https://www.vgemba.net/vmware/powershell/Saving-PowerCLI-Credentials/
- The Windows Update Powershell Module must be installed on the VM Template:
- Install-Module -Name PSWindowsUpdate
- #>
- #--------------------------------
- # Store Credentials:
- #--------------------------------
- <#
- .Run the below commands to store credentials if updating creds or running for the first time
- These must be ran from whatever account the scripts will run from (svc_vmware).
- The scheduled task will also need to be ran from this account.
- $VCenterCreds = Get-Credential
- $VCenterCreds | Export-Clixml -Path C:\Scripts\Credentials\VCenterCreds.cred
- $TemplateCreds = Get-Credential
- $TemplateCreds | Export-Clixml -Path C:\Scripts\Credentials\TemplateCreds-Administrator.cred
- #>
- #--------------------------------
- # Variables
- #--------------------------------
- $VCenterServer = "MyDomain-VCSA"
- $VCenterFQDN = "$VCenterServer.mydomain.net"
- $VCenterCreds = Import-Clixml -Path C:\Scripts\Credentials\VCenterCreds.cred # MyDomain\SVC_VMware
- $TemplateName = "tmpWIN2019"
- $TemplateCreds = Import-Clixml -Path C:\Scripts\Credentials\TemplateCreds-Administrator.cred # VM's local administrator
- $Date = Get-Date -Format "yyyy-MM-dd"
- $LogPath = "C:\Scripts\Logs\$VCenterServer.$TemplateName.Updates.$Date.log"
- #--------------------------------
- # Update Process
- #--------------------------------
- # Import the PowerCLI module
- Import-Module VMware.VimAutomation.Core -ErrorAction SilentlyContinue
- #Set to allow working with multiple servers
- Set-PowerCLIConfiguration -DefaultVIServerMode multiple -Confirm:$false
- # Connect to vCenter
- Connect-VIServer -Server $VCenterFQDN -Credential $VCenterCreds
- # Convert a template to a VM
- Set-Template -Template $TemplateName -ToVM -Confirm:$false -RunAsync
- # Make a 15 seconds delay
- Start-sleep -s 15 # 15 seconds
- # Temporary VM notes
- $VMnote = "VMAUTO - CURRENTLY UPDATING VM TEMPLATE. DO NOT TOUCH."
- Get-VM $TemplateName|Set-VM -Notes $VMnote -Confirm:$false
- # Start the virtual machine
- Start-VM -VM $TemplateName | Get-VMQuestion | Set-VMQuestion -DefaultOption -Confirm:$false
- Start-sleep -s 60
- Wait-Tools -VM $TemplateName # Wait for VMtools to load
- Start-sleep -s 15
- # Ensure Windows Update Powershell module is installed on VM
- Invoke-VMScript -ScriptType PowerShell -ScriptText "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12" -VM $TemplateName -GuestCredential $TemplateCreds | Out-file -Filepath $LogPath -Append
- Invoke-VMScript -ScriptType PowerShell -ScriptText "Install-PackageProvider NuGet -Force" -VM $TemplateName -GuestCredential $TemplateCreds | Out-file -Filepath $LogPath -Append
- Invoke-VMScript -ScriptType PowerShell -ScriptText "Install-Module -Name PSWindowsUpdate -Force" -VM $TemplateName -GuestCredential $TemplateCreds | Out-file -Filepath $LogPath -Append
- # Run the command to install all available updates in the guest OS using VMWare Tools (the update installation log is saved to a file)
- Invoke-VMScript -ScriptType PowerShell -ScriptText "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot" -VM $TemplateName -GuestCredential $TemplateCreds | Out-file -Filepath $LogPath -Append
- Start-sleep -s 30 # 30 seconds
- # Update VMTools version
- Update-Tools -VM $TemplateName -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 $TemplateName -GuestCredential $TemplateCreds
- Start-sleep -s 30 # 30 seconds
- # Force restart the VM
- Restart-VMGuest -VM $TemplateName -Confirm:$false
- Start-sleep -s 60
- Wait-Tools -VM $TemplateName # Wait for VMtools to load
- Start-sleep -s 15
- # Update VM Notes
- $VMnote = "VMAUTO - $((Get-Date -Format "yyyy/MM/dd HH:MM").ToString()) - Updated Windows and VMware tools."
- Get-VM $TemplateName|Set-VM -Notes $VMnote -Confirm:$false
- # Shut the VM down and convert it back to the template
- Shutdown-VMGuest -VM $TemplateName -Confirm:$false
- do { $vmstatus = (get-vm $TemplateName).PowerState ; Start-Sleep -Seconds 5} while ($vmstatus -ne "PoweredOff") # WAIT UNTIL POWERED OFF
- Set-VM -VM $TemplateName -ToTemplate -Confirm:$false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement