PowerCLI Get-VMToolsVersion

I recently was able to upgrade my VMware server so that I can now fully use the PowerCLI tool set. This is fantastic PowerShell goodness that I hope to use and write about much more in the future. Part of my upgrade process includes upgrading the VMToools install on the virtual machines. But what wasn’t so easy is to pull out the tools version currently installed. The information is there when using Get-View, but it takes a little bit of work. So naturally I developed my own solution.

I wrote a PowerShell 2.0 function that can work in a pipelined expression to return the tools version number.

Function Get-VMToolsVersion {
#requires -pssnapin VMWare.VimAutomation.Core            

<#
.Synopsis
    Get VMware Tools version for a given virtual machine.
.Description
    This function will add the ToolsVersion property to a virtual 
    machine object. The object is written to the pipeline but because 
    the default formatted view doesn't know about the property you need 
    to use Select-Object to display it.

.Parameter VM
    A virtual machine object.
.Example
    PS C:\> get-vmtoolsversion (get-vm researchdc) | select ToolsVersion

    Get the ToolsVersion for the ResearchDC virtual machine
.Example
    PS C:\> get-vm | get-vmtoolsversion | Sort ToolsVersion | 
      select Name,ToolsVersion, PowerState

    Presents a summary report of all virtual machines displaying the 
    virtual machine name, the version of VMware Tools, and its power 
    state.
.Example
    PS C:\> get-vm | get-vmtoolsversion | where {
      $_.ToolsVersion -lt 8194} | sort ToolsVersion | 
      select Name,ToolsVersion,PowerState

    Similar to the previous example except it returns only VMs with 
    tools version less than 8194. Output is sorted by the version.
.Inputs
    Virtual Machine object
.Outputs
    A customized Virtual Machine object
.Link
    Get-VM
    Get-View

.Notes
 NAME:      Get-VMToolsVersion
 VERSION:   1.0
 AUTHOR:    Jeffery Hicks
 LASTEDIT:  4/8/2010

#>            

[cmdletbinding()]            

Param (
     [Parameter(Position=0, Mandatory=$True, ValueFromPipeline=$True,
     HelpMessage="You must specify a virtual machine object")]
     [VMWare.VimAutomation.Client20.VirtualMachineImpl[]]$vm
    )            

Begin {
    Write-Verbose "Starting function"
}            

Process {
    Write-Verbose "Getting view for $($vm.name)"
    $view=Get-View -viObject $vm            

    $ToolsVersion=$view.Config.Tools.ToolsVersion
    write-verbose "Found tools version value of $ToolsVersion"
    $vm | Add-Member -MemberType NoteProperty -Name "ToolsVersion" `
-Value $toolsVersion -passthru            

}            

End {
    Write-Verbose "Ending function"
}            

} #end Function

The function takes a virtual machine object as a parameter and uses Get-View to retrieve the ToolsVersion property. I could have created a new custom object, but I decided to simply add this as a new property to the virtual machine object using Add-Member. Using –passthru means the object is written back to the pipeline. Because the default formatter doesn’t recognize this property, you have to explicitly ask for it. But that’s pretty easy and as you can see very helpful.

[vSphere PowerCLI] C:\Scripts> get-vm | get-vmtoolsversion | sort ToolsVersion |

format-table name,toolsversion -autosize

Name                                 ToolsVersion

—-                                 ————

Win2K8R2 Baseline                            7303

Win7 Baseline                                7303

MyCompany Exchange 2007                      8193

MyCompany Vista                              8193

ResearchDC                                   8193

Research Member Server R2                    8193

MyCompany Windows 7                          8193

R2 Core RODC                                 8193

R2 Server Core Baseline                      8193

MyCompany 2008                               8193

MyCompany XP                                 8194

MyCompanyDC 2K3R2                            8194

MyCompany XP Restored                        8194

MyCompany2003                                8194

The function includes comment based help, but is lacking any sort of robust error handling. I suppose I should add that in.

I’m still learning all the ins and outs of PowerCLI but this meets my needs and maybe it will meet yours.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to FriendFeed Post to Google Buzz Post to Ping.fm Post to Reddit Post to Slashdot Post to StumbleUpon Post to Technorati

This entry was posted in PowerCLI, PowerShell v2.0 and tagged , , , . Bookmark the permalink.

Comments are closed.