From a7ff78fee0f8a7d1d24ac7b04f0dc7ee70dec522 Mon Sep 17 00:00:00 2001 From: Kyle Ruddy Date: Mon, 18 Nov 2019 19:09:41 -0500 Subject: [PATCH] Create Get-TriggeredAlarm.ps1 Adding a new function to view triggered alarms for vSphere objects --- Scripts/Get-TriggeredAlarm.ps1 | 62 ++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Scripts/Get-TriggeredAlarm.ps1 diff --git a/Scripts/Get-TriggeredAlarm.ps1 b/Scripts/Get-TriggeredAlarm.ps1 new file mode 100644 index 0000000..a5d8dc6 --- /dev/null +++ b/Scripts/Get-TriggeredAlarm.ps1 @@ -0,0 +1,62 @@ +function Get-TriggeredAlarm { +<# + .SYNOPSIS + This function lists the triggered alarms for the specified entity in vCenter + .DESCRIPTION + List the triggered alarms for the given object + .NOTES + Author: Kyle Ruddy, @kmruddy, kmruddy.com + .PARAMETER VM + Specifies the name of the VM + .PARAMETER VMHost + Specifies the name of the VMHost + .PARAMETER Datacenter + Specifies the name of the Datacenter + .PARAMETER Datastore + Specifies the name of the Datastore + .EXAMPLE + Get-TriggeredAlarm -VM VMname + + Entity Alarm AlarmStatus AlarmMoRef EntityMoRef + ---- ---- ---- ---- ---- + VMname Name Yellow Alarm-MoRef Entity-MoRef +#> + + [CmdletBinding()] + param( + [string]$VM, + [string]$VMHost, + [string]$Datacenter, + [string]$Datastore + ) + BEGIN { + switch ($PSBoundParameters.Keys) { + 'VM' {$entity = Get-VM -Name $vm -ErrorAction SilentlyContinue} + 'VMHost' {$entity = Get-VMHost -Name $VMHost -ErrorAction SilentlyContinue} + 'Datacenter' {$entity = Get-Datacenter -Name $Datacenter -ErrorAction SilentlyContinue} + 'Datastore' {$entity = Get-Datastore -Name $Datastore -ErrorAction SilentlyContinue} + default {$entity = $null} + } + + if ($null -eq $entity) { + Write-Warning "No vSphere object found." + break + } + } + PROCESS { + if ($entity.ExtensionData.TriggeredAlarmState -ne "") { + $alarmOutput = @() + foreach ($alarm in $entity.ExtensionData.TriggeredAlarmState) { + $tempObj = "" | Select-Object -Property Entity, Alarm, AlarmStatus, AlarmMoRef, EntityMoRef + $tempObj.Entity = Get-View $alarm.Entity | Select-Object -ExpandProperty Name + $tempObj.Alarm = Get-View $alarm.Alarm | Select-Object -ExpandProperty Info | Select-Object -ExpandProperty Name + $tempObj.AlarmStatus = $alarm.OverallStatus + $tempObj.AlarmMoRef = $alarm.Alarm + $tempObj.EntityMoRef = $alarm.Entity + $alarmOutput += $tempObj + } + $alarmOutput | Format-Table -AutoSize + } + } + +} \ No newline at end of file