Upgrade VM Hardware Version after Guest OS reboot

Last week I was looking for a way to bundle VM Hardware Upgrades with our monthly Guest OS patching. After the Guest OS is patched, it gets rebooted. The goal was to get the VMs to upgrade their hardware after the Guest OS is rebooted. And avoid having to power of the VMs to carry out the hardware upgrades.

It appears this is possible by changing the VM UpgradePolicy to “Always” and setting a VersionKey (i.e. the ESX compatibility level) using Powershell.

To prove that changing these VM settings I mentioned above actually triggers a Hardware Upgrade at Guest reboot, I built a test VM, then carried out the steps below.

I’ve simply used the scripts provided in this blog post. https://blogs.vmware.com/vsphere/2018/09/automating-upgrade-of-vmware-tools-and-vmware-compatibility.html

Get VM Hardware Version and Upgrade Policy status prior to upgrade.

$VM = Get-View -VIObject "HWU-Test-01"
$VM.config.version
vmx-11
$VM.config.ScheduledHardwareUpgradeInfo
UpgradePolicy VersionKey ScheduledHardwareUpgradeStatus Fault
------------- ---------- ------------------------------ -----
never                    none
$VMConfig = Get-View -VIObject $VM.Name

Set UpgradePolicy (always) and VersionKey (vmx-15 > 6.7U2).

$VMConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$VMConfigSpec.ScheduledHardwareUpgradeInfo = New-Object -TypeName VMware.Vim.ScheduledHardwareUpgradeInfo
$VMConfigSpec.ScheduledHardwareUpgradeInfo.UpgradePolicy = “always”
$VMConfigSpec.ScheduledHardwareUpgradeInfo.VersionKey = “vmx-15”

Apply new Upgrade Policy and VersionKey.

$VMConfig.ReconfigVM($VMConfigSpec)

Get VM Hardware Version and Upgrade Policy status prior to upgrade.

$VM = Get-View -VIObject "HWU-Test-01"
$VM.config.version
vmx-11

UpgradePolicy has changed to always and VersionKey is now set.

$VM.config.ScheduledHardwareUpgradeInfo
UpgradePolicy VersionKey ScheduledHardwareUpgradeStatus Fault
------------- ---------- ------------------------------ -----
always        vmx-15     pending

Reboot Guest OS via vCenter to trigger Hardware Upgrade.

Get VM Hardware Version and Upgrade Policy status POST upgrade.

$VM = Get-View -VIObject "HWU-Test-01"
$VM.config.version
vmx-15

Upgrade successful but UpgradePolicy has reverted back to never.

$VM.config.ScheduledHardwareUpgradeInfo
UpgradePolicy VersionKey ScheduledHardwareUpgradeStatus Fault
------------- ---------- ------------------------------ -----
never                    success

Now that we know it works, we can simply run the following script for all the VMs managed by the vCenter server and wait for the VMs to get rebooted.

$HardwareUpdateVMs = Get-VM
Foreach ($VM in ($HardwareUpdateVMs)) {
$VMConfig = Get-View -VIObject $VM.Name
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.ScheduledHardwareUpgradeInfo = New-Object -TypeName VMware.Vim.ScheduledHardwareUpgradeInfo
$vmConfigSpec.ScheduledHardwareUpgradeInfo.UpgradePolicy = “always”
$vmConfigSpec.ScheduledHardwareUpgradeInfo.VersionKey = “vmx-15”
$VMConfig.ReconfigVM($vmConfigSpec)
}

NOTE – Upgrading a VM to the latest Hardware Version my end up breaking your VM. The success of upgrade depends on whether the Guest OS can handle the changes that come with the upgrade. For more on VM Hardware upgrades please visit the link below.

https://kb.vmware.com/s/article/1010675

One thought on “Upgrade VM Hardware Version after Guest OS reboot

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.