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:
[code language=”powershell”]
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
}
}
[/code]
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
[code language=”PowerShell”]
Get-VMHostSensorsInfo -VMHostName server1
[/code]
Get sensors status of multiple hosts
[code language=”PowerShell”]
Get-VMHostSensorsInfo -VMHostName server1, server2
[/code]
Get sensors status of hosts from file
[code language=”PowerShell”]
Get-VMHostSensorsInfo -VMHostName (Get-Content c:\temp\servers.txt)
[/code]
Export sensors status to CSV
[code language=”PowerShell”]
Get-VMHostSensorsInfo -VMHostName (Get-Content c:\temp\servers.txt) -ExportToCSV
[/code]
Hope this helps…
Comments on this entry are closed.
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
What is the error you are receiving?
Very nice script.
Thank you