Upgrading VMware Tools Via PowerCLI

We recently needed to update many of our clients VMware tools due to a security exploit that we became aware of. Many of our clients have lots of VMs in their environments so we decided to create a PowerCLI script to automate going through so many. The script below is what we used (you can also get it from the my GitHub page here):

 

## 1. Install Power CLI if not installed
## Install-Module VMware.PowerCLI -Scope CurrentUser -AllowClobber

## 2. Check the Power CLi version
## Get-PowerCLIVersion

## 3. To update remove first then install as per 1.
## Get-module VMware.* -listAvailable | Uninstall-Module -Force

## Variables
$VCServer=''
$DatacentreName=''
$VMList='C:\VMs with Outdated Tools.txt'
$logfile="C:\VMware Tools Update.log"

## Import PowerCLI module
Import-Module VMware.VimAutomation.Core

## Allows connection to vCenter with invalid cert
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

## Connect to vCenter
Connect-VIServer $VCServer

## Creates a txt doc with all VMs with VMware Tools
Get-Datacenter -Name $DatacentreName | get-vm | Where-Object -Property PowerState -eq 'PoweredOn'| get-vmguest | Select -Expandproperty VMName | FT -autosize | Out-File -FilePath $VMList

## Imports VMs from above txt file and updates VMware Tools without reboot
## Creates a log file with details of any errors
Start-Transcript -Path $logfile
$VMs = Get-Content $VMList
foreach ($VM in $VMs) {Get-VM -Name $VM | Update-Tools -NoReboot}
Stop-Transcript

## Use to check versions after upgrades
Get-VM | Where-Object -Property PowerState -eq 'PoweredOn'| Select-Object -Property Name,@{Name='ToolsVersion';Expression={$_.Guest.ToolsVersion}}

## Commands for installing via advanced if automatic fails
## /s /v "/qn REBOOT=ReallySuppress"

 

The first part contains the commands needed if PowerCLI isn’t installed.

 

## 1. Install Power CLI if not installed
## Install-Module VMware.PowerCLI -Scope CurrentUser -AllowClobber

## 2. Check the Power CLi version
## Get-PowerCLIVersion

## 3. To update remove first then install as per 1.
## Get-module VMware.* -listAvailable | Uninstall-Module -Force

 

In the variables section add your vCenter server name (I recommend using FQDN) and the name of the Datacentre you want to update: (Note: you can change the names of the log files from here also)

 

## Variables
$VCServer=''
$DatacentreName=''
$VMList='C:\VMs with Outdated Tools.txt'
$logfile="C:\VMware Tools Update.log"

 

You can either run the rest of the script all at once or section by section. If you do it section by section you can amend the list of VMs that get updated in the text file. 

Once you run the below code you should be prompted to login to your vCenter server and it will compile a list of the VM names to a text file on the root of the C: drive:

 

## Import PowerCLI module
Import-Module VMware.VimAutomation.Core

## Allows connection to vCenter with invalid cert
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

## Connect to vCenter
Connect-VIServer $VCServer

## Creates a txt doc with all VMs with VMware Tools
Get-Datacenter -Name $DatacentreName | get-vm | Where-Object -Property PowerState -eq 'PoweredOn'| get-vmguest | Select -Expandproperty VMName | FT -autosize | Out-File -FilePath $VMList

 

You can then either edit the file created and remove any machines you don’t want or use the list as is. It also acts as handy reference of what VMs will be attempted.

Next run this section to go through and update the VMware Tools on your environment:

 

## Imports VMs from above txt file and updates VMware Tools without reboot
## Creates a log file with details of any errors
Start-Transcript -Path $logfile
$VMs = Get-Content $VMList
foreach ($VM in $VMs) {Get-VM -Name $VM | Update-Tools -NoReboot}
Stop-Transcript

 

Any errors will be put into the log file on the C: drive that can be checked after. This may be due to out of date/non compatible OS versions (I had a Windows 2003 server that is no longer supported). Some may need Microsoft redistributables installed before it installs VMware tools. 

You can use the next section to get a list of the VMs and the version of VMware tools installed on them.

 

## Use to check versions after upgrades 
Get-VM | Where-Object -Property PowerState -eq 'PoweredOn'| Select-Object -Property Name,@{Name='ToolsVersion';Expression={$_.Guest.ToolsVersion}}

 

I’ve also included the commands that can be used in vCenter to install VMware tools via the the Automatic upgrade path option. (Ignore the ##)

 

## Commands for installing via advanced if automatic fails
## /s /v "/qn REBOOT=ReallySuppress"