PowerCLi:如何获取所有主机的健康状态

huangapple go评论62阅读模式
英文:

PowerCLi: how get Health status from all hosts

问题

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...
英文:

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

得分: 2

按照您的要求,这里是您代码的中文翻译:

按照要求这是我的回答评论

您可以简单地将所有内容放在一个循环中

    $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
            }
        }
    }

    # 在屏幕上输出
    $result | Format-Table -AutoSize

    # 输出到 CSV 文件
    $result | Export-Csv -Path 'X:\Somewhere\VMReport.csv' -NoTypeInformation
英文:

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

huangapple
  • 本文由 发表于 2023年5月13日 18:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定