6

Using PowerShell to update VMware tools on VM’s

VMware makes it easy to update VMware tools through the console, but clicking through hundreds of VM’s takes too much time. Thankfully VMware has provided us with the Update-Tools cmdlet so we can automate this process. We use folders pretty heavily in our environment and I like to roll out changes in small batches initially. In my case it makes sense to update VMware tools on VM’s in an individual folder (such as test).

I wrote the following PowerShell script to allow admins to update their tools on either individual folders or to the entire environment.

Instructions:

  • Modify $vcenter and enter your vCenter server name between the quotes
  • Run the script in PowerShell (The vSphere PowerCLI must be installed)
  • Enter the folder name you wish to update, otherwise leave this option blank to update all your VM’s
#Enter your vCenter Host below
$vcenter = "your_vcenter_server"
################################

#Load the VMware Powershell Module
Add-PSsnapin VMware.VimAutomation.Core

#Connect to the vCenter server defined above. Ignore certificate errors
Connect-VIServer $vcenter -wa 0

#Get the folder name from the user. Update all VM's if blank
$folder = Read-Host "Which folder would you like to update? (Leave blank to update all VM's)"

#Check if the user entered a folder. If a folder was provided return only the VM's for that folder
If($folder)
{
Write-Host "Updating VM's in $folder"
#Get all VM's for a specified folder and return only their names
$virtualmachines = Get-Folder $folder | Get-VM | select -expandproperty Name
}

#Else if the user left the folder blank, get all VM's
Else
{
Write-Host "Updating all VM's"
#Get all VM's in the vCenter and return only their names
$virtualmachines = Get-VM | select -expandproperty Name
}

#Perform the following for each VM returned
ForEach ($vm in $virtualmachines)
{
Write-Host "Updating VMware Tools on $VM"
#Update VMware tools without rebooting the VM
Update-Tools $vm -NoReboot
}

To see what guests and what folders have guests that need updating you can run the following command (borrowed from Grzegorz Kulikowski’s site and tweaked a little).


Get-VM|?{$_.Extensiondata.Summary.Guest.ToolsVersionStatus -like 'guestToolsNeedUpgrade'} | select name,folder,@{N='tools vers';E={$_.ExtensionData.Config.Tools.ToolsVersion}},@{N='Tools Status';E={$_.Extensiondata.Summary.Guest.ToolsVersionStatus}}

Remember if you’re running this in PowerShell you still need to load the VMware module and connect to your vCenter server first. Or if you’re using PowerCLI you just need to connect to your vCenter server.

Matt Bradford

6 Comments

  1. Hi, i need a script to fetch the VMs detail where VMtool is out-of-dated and without reboot i can update it

    • Hi Kuldeep,

      If you want to target only VM’s that have outdated tools, replace line 27 with the following…

      $virtualmachines = Get-VM | ?{$_.Extensiondata.Summary.Guest.ToolsVersionStatus -like ‘guestToolsNeedUpgrade’} | select -expandproperty Name

      There’s no harm in targeting all VM’s because the VMware tools installer will know whether or not an upgrade is necessary, it may just take a little longer. It’s all about preference.

      Cheers!
      Matt

  2. Hello, I need a script to find VMs where VMwaretools are not installed and to install tools without reboot. The update script works great! Thanks!

Leave a Reply