Automation

 View Only
Expand all | Collapse all

Powercli delete alarm

  • 1.  Powercli delete alarm

    Posted Mar 27, 2015 09:22 AM

    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 :smileyhappy:



  • 2.  RE: Powercli delete alarm

    Posted Mar 27, 2015 09:28 AM

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



  • 3.  RE: Powercli delete alarm

    Posted Mar 27, 2015 09:36 AM

    Hello PowerCLI Master :smileyhappy:

    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 :smileysad:



  • 4.  RE: Powercli delete alarm
    Best Answer

    Posted Mar 27, 2015 09:46 AM

    Does this do the trick ?

    $alarmName = 'MyAlarm'

    $alarmMgr = Get-View AlarmManager

    $alarm = Get-View ($alarmMgr.GetAlarm($null)) | where{$_.Info.Name -eq $alarmName}

    $alarm.RemoveAlarm()



  • 5.  RE: Powercli delete alarm

    Posted Mar 27, 2015 09:59 AM

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



  • 6.  RE: Powercli delete alarm

    Posted Mar 27, 2015 10:42 AM

    I updated the code above, please try again.



  • 7.  RE: Powercli delete alarm

    Posted Mar 27, 2015 10:53 AM

    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>



  • 8.  RE: Powercli delete alarm

    Posted Mar 27, 2015 12:49 PM

    I must be having an off-day :smileysad:

    Try the new code above.



  • 9.  RE: Powercli delete alarm

    Posted Mar 27, 2015 01:12 PM

    You're not the king of powercli for nothing, thanks man :smileyhappy:



  • 10.  RE: Powercli delete alarm

    Posted Mar 27, 2015 03:06 PM

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



  • 11.  RE: Powercli delete alarm

    Posted Mar 27, 2015 04:29 PM

    Thanks, I corrected it above as well



  • 12.  RE: Powercli delete alarm

    Posted Aug 16, 2018 02:48 PM

    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



  • 13.  RE: Powercli delete alarm

    Posted Aug 16, 2018 03:27 PM

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

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

    $alarm.ExtensionData.RemoveAlarm()



  • 14.  RE: Powercli delete alarm

    Posted Aug 17, 2018 02:36 AM

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

    }