PowerCLI: Get ESXi Hosts Version and Uptime

This is a quick and easy script to get all hosts from a vCenter sorted by Cluster and Host name. You will get the Cluster, Hostname, Version, Build and Uptime in days.

Connect-VIServer <vCenter FQDN>


$clusters = Get-Cluster | Sort-Object
$objects = @()

foreach ($cluster in $clusters) {
    Write-Host "Gathering from cluster: $($cluster.Name)"

    $vmhosts = $cluster | Get-VMHost | Sort-Object
    
    foreach ($vmhost in $vmhosts) {
        $object = New-Object -TypeName PSObject
        $object | Add-Member -MemberType NoteProperty -Name "Cluster" -Value $cluster
        $object | Add-Member -MemberType NoteProperty -Name "Host" -Value $vmhost
        $object | Add-Member -MemberType NoteProperty -Name "Version" -Value $vmhost.Version
        $object | Add-Member -MemberType NoteProperty -Name "Build" -Value $vmhost.Build
        $object | Add-Member -MemberType NoteProperty -Name "Uptime (Days)" -Value (New-TimeSpan -Start $vmhost.ExtensionData.Summary.Runtime.BootTime -End (Get-Date) | Select-Object -ExpandProperty Days)
        $objects += $object
    }
}

$objects | ft -AutoSize

Related Post