VMware + PowerShell = AWESOMENESS!!!

In my 2nd “real” powershell script I have decided to explore the sheer awesomeness that is PowerCLI.  Having recently upgraded a client to vSphere I suddenly had a bunch of VMs with outdated tools that needed to be upgraded including one host with 60 VM’s (that’s a lot of right-clicking and installing tools…).  So, PowerShell comes to the rescue again!  This script connects to a VI3 or vSphere host, queries all running VMs for tools status and runs the tools update on out of date VMs with a one minute delay. Enjoy!

As always, comments, suggestions, corrections and requests are always welcome :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#************************************************************************************
#
# NAME: UpdateVMTools.ps1
#
# AUTHOR: Allen Oliver
#
# EMAIL: admin@evilemuofdoom.com
#
# COMMENT:
# This script will login to a single VI3 or vSphere Host, make a list of VM's with
# "Out of Date" Tools and update them (reboot required). It also creates a VERY
# simple log file that mainly provides a list of VM's that were/weren't updated
# Feel free to modify or use this script in any way. Comments, Suggestions,
# and/or Corrections are always welcome!
#
# SPECIAL THANKS!:
# Thanks goes out to Hal (www.halr9000.com) and LucD who's blog / forum posts
# provided the majority of information needed as well as some of the command
# content required for this script. If you haven't already, please checkout
# http://halr9000.com and search for LucD in the VMware Community forums.
#
# VERSION HISTORY:
# 1.0 - 8/05/2009 - Wrote and tested script
# 1.1 - 8/19/2009 - Removed "noreboot" option (no longer a valid install flag)
# 1.2 - 8/20/2009 - Fixed Name output to logfile
#
#************************************************************************************

cls
#Prompt for VI Host and login credentials, set logfile name to VMToolsUpdateLog-MM-DD-YYYY.log in C:\Scripts folder
$VIhost = Read-Host "Enter the name of the VI Host Server you would like to connect to"
$Cred = Get-Credential
$logFile = "C:\Scripts\VMToolsUpdateLog-$((get-date).ToString('MM-dd-yyyy')).log"
$Time = (Get-Date -format g)
cls
Add-Content "$Time - VMWare Tools Update started on $VIhost" -Path $logFile -Passthru

#Connect to VI Host and if connection fails, report $Error to log file and stop script
Connect-VIServer -Server $VIhost -Credential $Cred
If($? -eq $false){
Add-Content "$Time - $Error" -Path $logFile
Add-Content "Connection to $VIhost FAILED" -Path $logFile -Passthru
Add-Content "----------------------------------------------------------------------------------" -Path $logFile
Break
}

#Get list of VM's that are Powered ON and have "out of date" VMWare Tools installed
$VMs = get-vm | Where-Object { $_.powerstate -eq "PoweredON" } | foreach { get-view $_.ID } | Where-Object { $_.guest.toolsstatus -match "toolsOld" }

#If it returns no VM's with out of date tools write to log file and stop script
IF($VMs -eq $null){
Add-Content "$Time - VMWare Tools are up to date for all VM's currently powered on" -Path $logFile -Passthru
Add-Content "Completed on $Time" -Path $logFile -Passthru
Add-Content "----------------------------------------------------------------------------------" -Path $logFile
break
}

#Update VMware Tools and log results
foreach($vm in $VMs) {
$Name = $vm.Name
$vm.UpgradeTools_Task('/s /v/qn')
IF ($?) {
Add-Content "$Time - VMWare Tools Upgrade succuessfully started for $Name" -Path $logFile -PassThru
}
Else {
Add-Content "$Time - VMWare Tools Upgrade FAILED for $Name" -Path $logFile -PassThru
Add-Content "$Time - $Error" -Path $logFile -Verbose
}
#60 Second Delay between installs
Start-sleep -s 60
}

#Log script completion
Add-Content "Completed on $Time" -Path $logFile -Passthru
Add-Content "----------------------------------------------------------------------------------" -Path $logFile

Leave a Comment