In vSphere Client on a VM host in the Alarms tab there is a view of Triggered Alarms.
How can I get those alarms using PowerCLI?
In vSphere Client on a VM host in the Alarms tab there is a view of Triggered Alarms.
How can I get those alarms using PowerCLI?
this is what i use.
$esx = Get-VMHost $vmhost | Get-View
foreach($triggered in $esx.TriggeredAlarmState){
$alarmDef = Get-View -Id $triggered.Alarm
Write-Host $alarmDef.Info.Name
}
To find all vmhosts with alarm status = "red", I expanded on this.
#
# Find all hosts with triggered alarms in "Red" state
#
$esx_all = Get-VMHost | Get-View
$Report=@()
foreach ($esx in $esx_all){
foreach($triggered in $esx.TriggeredAlarmState){
If ($triggered.OverallStatus -like "red" ){
$lineitem={} | Select Name, AlarmInfo
$alarmDef = Get-View -Id $triggered.Alarm
$lineitem.Name = $esx.Name
$lineitem.AlarmInfo = $alarmDef.Info.Name
$Report+=$lineitem
}
}
}
$Report |Sort Name | export-csv "c:\temp\ESX-Host-Red-Alarms.csv" -notypeinformation -useculture
Invoke-item "c:\temp\ESX-Host-Red-Alarms.csv"
To expand a bit on johnshen64's PowerCLI answer, I found Luc Dekens' post on communities.vmware.com to be very helpful:
foreach($dc in (Get-Datacenter | where {$_.ExtensionData.triggeredAlarmState})){
$dc.ExtensionData.triggeredAlarmState |
Select @{N="Entity";E={Get-View $_.Entity | Select -ExpandProperty Name}},
@{N="Alarm";E={Get-View $_.Alarm | Select -ExpandProperty Info | Select -ExpandProperty Name}},Time,OverallStatus,
@{N='vCenter';E={$dc.Uid.Split('@:')[1]}}
}
... and I ended up using a variation of that:
foreach ($toplevel in @(Get-VDSwitch | Where-Object { $_.ExtensionData.TriggeredAlarmState.count -gt 0 }) +
@(Get-Datacenter | Where-Object { $_.ExtensionData.TriggeredAlarmState.count -gt 0 })) {
foreach ($alarm in $toplevel.ExtensionData.TriggeredAlarmState) {
$localt = [System.TimeZoneInfo]::ConvertTime($alarm.time, [System.TimeZoneInfo]::Local)
$entity = Get-View $alarm.Entity
$alarmview = Get-View $alarm.Alarm
Write-Output ("Alarm: At top-level entity $($toplevel.Name), $($entity.Name) went " +
"$($alarm.OverallStatus) at $($localt) for $($alarmview.Info.Name)")
}
}
The idea is to loop through each top-level entity that the alarms could roll up to. I discovered through trial and error that Get-VDSwitch
and Get-Datacenter
covered what I needed. The other tricky part is to realize that the Entity and Alarm objects inside the alarm are references to the actual objects, so to get details about the actual item, you have to dereference it with Get-View
.