PowerCLi: how get Health status from all hosts

603 views Asked by At

I have this working main script to get health status of one ESX host

$VMHostName = "Host01"

$HostView = Get-VMHost -Name $VMHostName | Get-View
$HealthStatusSystem = Get-View $HostView.ConfigManager.HealthStatusSystem
$SystemHealthInfo = $HealthStatusSystem.Runtime.SystemHealthInfo
ForEach ($Sensor in $SystemHealthInfo.NumericSensorInfo) {
  $Report = "" | Select-Object VMHost,Sensor,Status
  $Report.VMHost = $VMHostName
  $Report.Sensor = $Sensor.Name
  $Report.Status = $Sensor.HealthState.Key
  $Report
}


Output>

VMHost                                   Sensor                                                           Status
------                                   ------                                                                 ------
host01 PCI Bus 1 32-PCI 2-I/O mod --- Normal                                                  green 
host01 PCI Bus 1 30-PCI 1-I/O mod --- Normal                                                  green 
...

How can I improve this script to get status from all ESX hosts ? For getting all hosts list I use this command:

$VMHostName = (Get-View -ViewType HostSystem -Property Name,Config.Product).Name

$VMHostName
host01
host02
host03
...

Problem is when I re-run the main script, I get this weird output:

host01,host02,host03,host04...
host01,host02,host03,host04...
host01,host02,host03,host04...
1

There are 1 answers

0
Theo On BEST ANSWER

As requested here my comment as answer

You could simply put it all in a loop:

$result = foreach ($VMHostName in (Get-View -ViewType HostSystem -Property Name,Config.Product).Name) { 
    $HostView = Get-VMHost -Name $VMHostName | Get-View
    $HealthStatusSystem = Get-View $HostView.ConfigManager.HealthStatusSystem
    $SystemHealthInfo = $HealthStatusSystem.Runtime.SystemHealthInfo
    foreach ($Sensor in $SystemHealthInfo.NumericSensorInfo) {
        [PsCustomObject]@{
            VMHost = $VMHostName
            Sensor = $Sensor.Name
            Status = $Sensor.HealthState.Key
        }
    }
}

# output on screen
$result | Format-Table -AutoSize

# output to csv file
$result | Export-Csv -Path 'X:\Somewhere\VMReport.csv' -NoTypeInformation