PowerCLI script: schedule VMs to upgrade the virtual hardware at the next reboot

I had the requirement to upgrade the virtual hardware of a large number of VMs to the latest available version.

The following PowerCLI script schedules an upgrade of the vHW listed in a .txt file.

The upgrade will take place the next time when the VM is restarted. So you can prepare the schedule and during the next maintenance window (eg. when OS patches are deployed) the magic of the vHW upgrade happens 🙂

How to schedule a VM compatibility Upgrade for a list of VMs using PowerCLI:

 

    • prepare a list (.txt file) with the VMs like this:
    • use the following PowerCLI code to schedule the upgrade for the VMs listed in the .txt file:

      foreach($vmlist in (Get-Content -Path C:\TEMP\vmliste.txt)){

      $vm = Get-VM -Name $vmlist

                      $do = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec

                      $do.ScheduledHardwareUpgradeInfo = New-Object -TypeName VMware.Vim.ScheduledHardwareUpgradeInfo

                      $do.ScheduledHardwareUpgradeInfo.UpgradePolicy = “always”

                      $do.ScheduledHardwareUpgradeInfo.VersionKey = “vmx-11”

                      $vm.ExtensionData.ReconfigVM_Task($do)

      }

Do not forget to adapt the path to your .txt file. If necessary you can change the vHW version (in the script: vmx-11)

If you take a look at the vSphere Webclient you can find a schedule for the VM compatibility upgrade. It will take place during the next reboot of the VM.

Credits: I adapted scripts provided by LucD in different threads/forums to get this job done

Leave a Comment

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