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

2 Comments

  1. delchaleche

    What happened to Update Manager? It used to have a feature where you could set that in vCenter. Has that feature been removed? is there a difference between this and Update Manager feature in vCenter.

  2. Zachary Abbott

    Would love for this to work, but at line:

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

    I get the error:

    The property ‘ScheduledHardwareUpgradeInfo’ cannot be found on this object. Verify that the property exists and can be set.
    At line:1 char:17
    + … $do.ScheduledHardwareUpgradeInfo = New-Object -TypeName V …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

Leave a Comment

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