PowerShell One-Liner: Updating VMware Tools on Multiple Windows Guests Asynchronously

Updating VMware ESXi is a fairly painless task, especially when using vCenter with patch baselines.  One of the more annoying tasks is having to go through virtual machines and ensure that their VMware Tools are update-to-date after an upgrade.  Thankfully, this task can be automated with some PowerShell and VMware PowerCLI.

First, the modules need to be imported.

# Import the VMware Modules
Get-Module -ListAvailable | Where-Object { $_.Name -Like "VMware*" } | Import-Module

The VMware PowerCLI shortcut can be used which launches a custom PowerShell environment with the modules preloaded.

This one-liner makes use of the pipeline to get VMs in a given location, and retrieves their extension data.  This data contains a property under “Guest” called “ToolsVersionStatus”.  VMs with extension data that contains the string “guestToolsNeedUpgrade” will need their tools to updated.  In addition, the powers state of the VM needs to be on.  Since most Linux VMs use the open-vm-tools package, this one-liner will also query for Windows guests.  The results of this query should be piped into the Update-Tools cmdlet, a handy cmdlet included in VMware.VimAutomation.Core module.

# Get all Windows VMs that need updated tools, then update all tools at once
Get-VM -Location 'MyDatacenter' | Where-Object { $_.ExtensionData.Guest.ToolsVersionStatus -eq 'guestToolsNeedUpgrade' -and $_.PowerState -like 'PoweredOn' } | Get-VMGuest | Where-Object { $_.GuestFamily -like 'WindowsGuest'} | Update-Tools -NoReboot -RunAsync

The beautiful part of the Update-Tools cmdlet is that it includes a -NoReboot parameter.  This is the same as running setup64.msi /S /v “/qn REBOOT=R” on the command line inside the Windows guest.  Rebooting will be suppressed so that critical services can remain online.  The updated version of VMware Tools will still reflect both the guest OS and in ESXi.

The second parameter, -RunAsync, allows multiple tasks to run asynchronously.  A new task will be created and executed for each virtual machine retrieved from the pipeline at the same time. This option may not be recommended depending on the number of virtual machines in the environment.  If the -RunAsync option is not specified, each task will be executed individually.

Jimmy McNatt
Jimmy McNatt
Articles: 38