
Every time you update vSphere and/or vCenter you generally will need to update VMware Tools on all of your virtual machines as well. To make things a little easier I created a script that will do this automatically. The script is short, sweet, and rather effective. Let’s take a look!
First, it prompts for some information; the IP or FQDN of your vCenter server and then some credentials. When I first wrote this script it just used the logged on user’s credentials and that was fine because I was using Domain auth and my user account had the right permissions to run it. I’ve since changed jobs and have updated the script to prompt for credentials and then store those has variables to be used later.
#Get the name of the vCenter Server$vcenter = Read-Host "Enter the FQDN or IP of your vCenter Server"
#Get credentials for logging into vCenter$credential = Get-Credential$Username = $credential.GetNetworkCredential().username$Password = $credential.GetNetworkCredential().password
#Connect to the vCenter Server - Assuming the script is running under credentials that can connectConnect-VIServer $vcenter -User $Username -Password $Password
Next, after a successful connection, we get a list of VMs and then based on what we find we either update VMware tools or we skip over the VM, and avoid failed attempts, using a for each loop.
#Get a list of the virtualmachines that are Powered On$virtualmachines = Get-VM | Where-Object PowerState -eq "PoweredOn" | Select-Object -ExpandProperty Name
#Get Only the Windows VMsForEach ($vm in $virtualmachines){#If the VM is a Windows VM...
If (Get-VMGuest $vm | where-object OSFullName -like "Microsoft*") {
#Update VMware Tools but don't reboot the machine - Reboot may still occur depending on ESXi/vCenter version
Write-Host "Updating VMware tools on $vm"
Update-Tools $vm -NoReboot }
#If there is no OSFullName detected
elseif(Get-VMGuest $vm | Where-Object OSFullName -Like " *")
{
Write-Host "VMware Tools doesn't appear to be installed on $vm"
}
#If it's not a Windows VM then automatic update is not supported
else {
Write-Host "$vm is not a Windows VM and automatic Update is not supported"
}
}
So, first we look for VM’s that are powered on and running. Then, we look for VM’s that have Microsoft in the OSFullName, since updating VMware Tools in this way is only supported on Windows VMs. When updating VMware Tools I use the -NoReboot switch, however depending on the update being applied it may reboot anyway.
If the VM has anything else in the OSFullName column then it skips right over the VM and assumes its not a supported OS. If there is no text for the OSFullName it’s safe to assume that VMware tools is not installed in the first place, and a message is displayed letting you know this result.
If you’re interested in grabbing a copy of this you can head over to my Git Hub page. I admit – I’m not a PowerCLI expert. So, if you see a better/easier way to do what I’m trying to do here, by all means contribute to the code.