VMware Cloud Community
Sharantyr3
Enthusiast
Enthusiast
Jump to solution

Powercli delete alarm

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 Smiley Happy

Tags (2)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

Let's start with the begin, which code did you use to create the alarm ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sharantyr3
Enthusiast
Enthusiast
Jump to solution

Hello PowerCLI Master Smiley Happy

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 Smiley Sad

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
Sharantyr3
Enthusiast
Enthusiast
Jump to solution

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()

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I updated the code above, please try again.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sharantyr3
Enthusiast
Enthusiast
Jump to solution

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>

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I must be having an off-day Smiley Sad

Try the new code above.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Sharantyr3
Enthusiast
Enthusiast
Jump to solution

You're not the king of powercli for nothing, thanks man Smiley Happy

Reply
0 Kudos
Sharantyr3
Enthusiast
Enthusiast
Jump to solution

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()

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks, I corrected it above as well


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ustcsh
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you know the name of the alarm, you could do

$alarm = Get-AlarmDefinition -Name 'Test Alarm'

$alarm.ExtensionData.RemoveAlarm()


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

ustcsh
Contributor
Contributor
Jump to solution

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()

}

Reply
0 Kudos