≡ Menu

PowerShell: Get VM Host Hardware sensors status information using PowerCLI

VMware ESX host stores good amount of information about Hardware and it sensor’s values. These values are extremely useful in troubleshooting any peformance/hardware issues. In this article, I will show you how to query these values using PowerCLI.

This function assumes that you already connected to VCenter. You need to provide list of VMhost names as parameter to this script. The script also takes optional -ExportToCSV in case you want to export the data to CSV file for further processing.

Code:

function Get-VMHostSensorsInfo {
[CmdletBinding()]
param(
    [parameter(mandatory=$true)]
    [string[]]$VMHostName,
    [switch]$ExportToCSV
)

    $OutArr = @()
    foreach($VMHost in $VMHostName) {
        Write-Verbose "Fetching data from $VMHost"
        try {
            $VMHostObj = Get-VMHost -Name $VMHost -EA Stop | Get-View
            $sensors = $VMHostObj.Runtime.HealthSystemRuntime.SystemHealthInfo.NumericSensorInfo
            foreach($sensor in $sensors){
                $object = New-Object -TypeName PSObject -Property @{
                    VMHost = $VMHost
                    SensorName = $sensor.Name
                    Status = $sensor.HealthState.Key
                    CurrentReading = $sensor.CurrentReading 
                } | Select-Object VMHost, SensorName, CurrentReading, Status
                $OutArr += $object
            }
         } catch {
            $object = New-Object -TypeName PSObject -Property @{
                VMHost = $VMHost
                SensorName = "NA"
                Status = "FailedToQuery"
                CurrentReading = "FailedToQuery"
            } | Select-Object VMHost, SensorName, CurrentReading, Status
            $OutArr += $object
         }

    }

    if($ExportToCSV) {
        Write-Verbose "Exporting to c:\temp\sensorsinfo.csv"
        $OutArr | export-csv c:\temp\sensorsinfo.csv -NoTypeInformation
    } else {
        return $OutArr
    }
}

Usage:

Using this code is very simple. First open your PowerCLI and connect the VCenter using the credentials that can query the host information. Then copy and paste the above function to the PowerCLI shell and start running commands below as per your needs.

Get Sensors status of single host

Get-VMHostSensorsInfo -VMHostName server1

Get sensors status of multiple hosts

Get-VMHostSensorsInfo -VMHostName server1, server2

Get sensors status of hosts from file

Get-VMHostSensorsInfo -VMHostName (Get-Content c:\temp\servers.txt)

Export sensors status to CSV

Get-VMHostSensorsInfo -VMHostName (Get-Content c:\temp\servers.txt) -ExportToCSV

Hope this helps…

Comments on this entry are closed.

  • Anandaraj January 31, 2019, 11:48 am

    I am getting the error while running single host or multiple host.
    could you provide the command line with steps to run on multiple ESXi using the script

    • Wintel Rocks February 16, 2019, 5:56 pm

      What is the error you are receiving?

  • Jochen March 22, 2021, 3:06 pm

    Very nice script.
    Thank you