PoshCode Logo PowerShell Code Repository

Update VM Tools (modification of post by Brian English view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/521"></script>download | new post

A VI toolkit script to update all VM Guests on the selected hosts

  1. ########################################################
  2. # Created by Brian English
  3. #   Brian.English@charlottefl.com
  4. #   eddiephoenix@gmail.com
  5. #
  6. # for Charlotte County Government
  7. # No warranty suggested or implied
  8. ########################################################
  9. # Purpose: Cycle through all VMs on a Virtualcenter Server
  10. #          and update the VMTools after Virtualcenter update
  11. ########################################################
  12. # Notes:   VMware Tools must be installed prior to updating
  13. #          it. After VMware Tools is updated, the virtual
  14. #          machine is restarted if needed. See Vi Toolkit
  15. #          CmdLets Reference: Update-Tools
  16. ########################################################
  17. #connect to VirtualCenter server (i.e. virtual-center-1)
  18.  
  19. if ($args[0] -eq $null)
  20. {$hostName = Read-Host "What host do you want to connect to?"}
  21. else
  22. {$hostName = $args[0]}
  23.  
  24. #connect to selected Virtualcenter or host server
  25. Connect-VIServer $hostName
  26.  
  27. ########################################################
  28. #get all vms listed in virtualcenter
  29. $vms = get-vm
  30.  
  31. ########################################################
  32. #check power state on each vm
  33. #if 'On' update tools
  34.  
  35. Foreach ($i in $vms)
  36. { #if virtualcenter is virtualized skip it
  37.   # a system restart would stop this script
  38.   if ((get-vm -name $i).Name -eq $hostname)
  39.   {"Skipping " + $hostname}
  40.   #if primary DC or DNS is virtualized skip it too
  41.   #elseif ((get-vm -name $i).Name -eq "DNS/DC/DHCP")
  42.   #{"Skipping DNS/DC/DHCP server name"}
  43.   else
  44.   {
  45.     if ((get-vm -name $i).PowerState -eq "PoweredOn")
  46.     { $i
  47.       Get-Date -format "hh:mm:ss"
  48.       update-tools -guest (get-vmguest -vm (get-vm -name $i))
  49.       Get-Date -format "hh:mm:ss"
  50.     }#if
  51.   #}#if
  52. }#foreach

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me