Monthly Archives: September 2012

Gather VMHost information using vSphere PowerCLI

To gather some basic information the Get-VMHost Cmdlet offers a wealth of information. Most basic information is easily accessible using the following command:

Get-VMHost

For the purpose of this article, we are looking for some identifying information regarding our ESX hosts. The attributes that we are after are the following:

  • Hostname
  • ESX Version and Build number
  • vSphere Uid
  • Hardware Uuid
  • The parent folder/cluster or data center which contains the ESX hot

Since these fields are scattered around, the following piece of code can be used to gather this info:

Get-VMHost | ForEach-Object {
    $_.Name
    $_.ExtensionData.Summary.Config.Product.FullName
    $_.Uid
    $_.ExtensionData.Hardware.SystemInfo.Uuid
    $_.Parent.Name
}

It does take a bit of effort to locate the data in this fashion, but once found it can make a considerable difference. For example if I wanted to gather the host name of the ESX host, the cluster in which it is located and the datacenter in which the cluster is stored, the following commands could be executed:

Get-VMHost Server01* | Select-Object -ExpandProperty Name | Tee-Object -Variable Server
Get-Cluster -VMHost $Server | Select-Object -ExpandProperty Name
Get-DataCenter -VMHost $Server | Select-Object -ExpandProperty Name

This can be shortened to a single command with a ForEach-Object statement:

Get-VMHost Server01* | ForEach-Object{
    $_.Name
    $_.Parent.Name
    $_.Parent.ParentFolder.Parent.Name
}

This has the advantage that only a single Cmdlet is used to retrieve the data from vCenter which makes the code easier to write and faster to execute, especially in large environments.

New article posted on PowerShell Magazine

PowerShell Magazine has posted another article of mine. The article explains how to use the System.Net.Dns .Net class to resolve host names and IP addresses.

Have a look at the article here:
PowerShell Magazine: #PSTip Resolve IP Address or a host name using .NET Framework