英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论