Recently I got asked to create an overview for a license audit we were receiving. Nothing all to difficult is what I thought at first. But then I had a look at the requirements and they were asking for the ESXi install date for each host.

Well when you have a couple hundred hosts in your environment, you do not want to login to each host and run a command. There is even a difference in commands that can be used when you have different versions of ESXi hosts in your environment, which makes it even more tedious. The below commands are examples that can be used to find your ESXi install date, all of these can be run on the ESXi shell:

ESXi 5.x and 6.0 hosts:

echo -n "ESXi install date: " ; date -d @$(printf "%d" 0x$(esxcli system uuid get | cut -d \- -f1 )) 
ESXi 5.x – 6.0 command

ESXi 6.5 hosts:

esxcli system stats installtime get
ESXi 6.5 command

Fortunately, since vSphere 6.5 there is a new API call that can be used to fetch the ESXi install date, so that you can instead do this scripted on each ESXi host. I actually tried to do this with the Putty Plink functionality but I failed, I couldn’t get it to work. It somehow couldn’t escape some characters that are needed to get the result.  

But I did get to make a script that does the trick with PowerCLI and uses both of the commands above and gives me all the data that is needed for mixed environments with ESXi 6.0 and ESXi 6.5. If you want to use this script you should edit the Clusternames and the Export-CSV file locations.

$Clusters = "cluster01", "cluster2"

$HostInfoTotal = @()
$ClusterInfoTotal = @()
$VMInfoTotal = @()

foreach ($Cluster in $Clusters){

	$ClusterInfo = Get-Cluster -Name $Cluster | Select Name, @{N="Num Hosts";E={$_.Extensiondata.Summary.NumHosts}}, @{N="DRS Enabled";E={$_.ExtensionData.Configuration.DrsConfig.Enabled}} 
	$ClusterInfoTotal += $ClusterInfo
	
	$HostInfo = Get-Cluster -Name $Cluster | Get-VMHost | Select Name, @{N="Num Proc.";E={$_.Extensiondata.Hardware.CpuInfo.NumCpuPackages}}, NumCPU, @{N="Cluster Name";E={$_.Parent}}, ProcessorType, @{N="Install Date";E={if ($_.Version -eq "6.5.0"){
	(Get-View ($_.ExtensionData.ConfigManager.ImageConfigManager)).InstallDate()
	}
	else{
	Get-VMHost -Name $_ | Select @{N='Install Date';E={$script:esxcli = Get-EsxCli -VMHost $_ -V2
        $epoch = $script:esxcli.system.uuid.get.Invoke().Split('-')[0] 
		[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds([int]"0x$($epoch)"))}}
		}
	}
	}
	$HostInfoTotal += $HostInfo
	
	$VMInfo = Get-Cluster -Name $Cluster | Get-VM | Select Name, @{N="Guest OS";E={$_.Extensiondata.Guest.GuestFullName}}, NumCPU, VMHost, @{N="Cluster";E={Get-Cluster -VM $_}}, PowerState
	$VMInfoTotal += $VMInfo
}
$ClusterInfoTotal | Export-Csv -Path "C:\temp\clusterinfototal.csv" -NoTypeInformation -UseCulture
$HostInfoTotal | Export-Csv -Path "C:\temp\hostinfototal.csv" -NoTypeInformation -UseCulture
$VMInfoTotal | Export-Csv -Path "C:\temp\vminfototal.csv" -NoTypeInformation -UseCulture

The only thing that is missing here is the fact that I’m also getting the object name for the ESXi 6.0 result, but I can live with that. I couldn’t fix that in the time that I had.

Script “hostinfototal.csv” preview

The script uses a vSphere Clustername as input and then creates an overview for each cluster in three different comma separated files (csv’s). Unfortunately the “Export-CSV” cmdlet doesn’t let me merge these overviews in one excel file with multiple sheets, so this was the quickest way around that issue. The script uses ESXCLI embedded into PowerCLI for the ESXi 6.0 hosts and uses the new ESXi 6.5 API call for those hosts.

There you have it! A script that helps you create some overviews that are needed for certain licensing audits.

Want to know more PowerCLI tips and tricks? Click on the button below:


Bryan van Eeden

Bryan is an ambitious and seasoned IT professional with almost a decade of experience in designing, building and operating complex (virtual) IT environments. In his current role he tackles customers, complex issues and design questions on a daily basis. Bryan holds several certifications such as VCIX-DCV, VCAP-DCA, VCAP-DCD, V(T)SP and vSAN and vCloud Specialist badges.

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *