Hi,
I'm doing tests to move alarms from one vcenter to another and got a small bug I guess.
I copied an alarm and I can't see it in the gui nor web client. I couldn't find any powercli method to delete an alarm ?
I can't see it because it s an alarm that monitor Datacenter object and I copied it to a vm folder.
I can view it from powershell using get-alarmdefinition and I know it's in my vm folder, but I don't know how to delete it.
Thanks for any help
Does this do the trick ?
$alarmName = 'MyAlarm'
$alarmMgr = Get-View AlarmManager
$alarm = Get-View ($alarmMgr.GetAlarm($null)) | where{$_.Info.Name -eq $alarmName}
$alarm.RemoveAlarm()
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Let's start with the begin, which code did you use to create the alarm ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello PowerCLI Master
I used
http://www.vnoob.com/2013/03/copy-alarms-from-one-vcenter-to-another/
USing http://www.peetersonline.nl/2009/10/report-vsphere-alarms-with-powershell/
I can see the alarm is located in my folder (vm type) named "toto" :
Folder | : toto |
Name | : alarme-sa-mere |
Description : rooooh
Enabled | : True |
Summary | : |
Comparisons | : | |
EventType | : AlarmEmailFailedEvent | |
EventTypeId | : | |
ObjectType | : Datacenter | |
Status | : red | |
DynamicType | : | |
DynamicProperty : |
But can't see it in the gui
Does this do the trick ?
$alarmName = 'MyAlarm'
$alarmMgr = Get-View AlarmManager
$alarm = Get-View ($alarmMgr.GetAlarm($null)) | where{$_.Info.Name -eq $alarmName}
$alarm.RemoveAlarm()
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $alarm = $alarmMgr.GetAlarm() | where{$_.Inf o.Name -eq $alarmName} Cannot find an overload for "GetAlarm" and the argument count: "0". At line:1 char:32 + $alarm = $alarmMgr.GetAlarm <<<< () | where{$_.Info.Name -eq $alarmName} + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest
I tried using your script and modifying like this but same results :
$alarmName = 'alarme-sa-mere'
$alarmMgr = Get-View AlarmManager
$alarm = $alarmMgr.GetAlarm() | where{$_.Info.Name -eq $alarmName}
$alarm.RemoveAlarm()
I updated the code above, please try again.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $alarm.RemoveAlarm()
You cannot call a method on a null-valued expression.
At line:1 char:19
+ $alarm.RemoveAlarm <<<< ()
+ CategoryInfo : InvalidOperation: (RemoveAlarm:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
In fact the $alarm variable was always empty so I modifed the where clause to catch the alarm internal name (alarm-102) but still get an error :
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $alarm = $alarmMgr.GetAlarm($null) | where{$_.va
lue -eq "alarm-102"}
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $alarm
Type Value
---- -----
Alarm alarm-102
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $alarm.removealarm()
Method invocation failed because [VMware.Vim.ManagedObjectReference] doesn't contain a method named 'removealarm'.
At line:1 char:19
+ $alarm.removealarm <<<< ()
+ CategoryInfo : InvalidOperation: (removealarm:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>
I must be having an off-day
Try the new code above.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You're not the king of powercli for nothing, thanks man
Just a quick correction for future viewers because I corrected it but forgot to tell :
$alarmName = 'MyAlarm'
$alarmMgr=Get-View AlarmManager
$alarm = Get-View ($alarmMgr.GetAlarm($null)) | where{$_.Info.Name -eq $alarmName}
$alarm.RemoveAlarm()
Thanks, I corrected it above as well
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc, this code snippets work for me, but looks like it is very time consuming during alarm deleting.
Probably because it needs to query all the alarms ($null) first ? How to improve it and make the deleting faster ?
Thanks
If you know the name of the alarm, you could do
$alarm.ExtensionData.RemoveAlarm()
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Million Thanks, Luc. looks better now : ) I also added 'SilentlyContinue' option just in case the alarm does not exist.
# Remove alarms first if already exist
$alarm= Get-AlarmDefinition -Name 'Test Alarm' -ErrorAction SilentlyContinue
if($alarm.Count -gt 0) {
$alarm.ExtensionData.RemoveAlarm()
}