VMware Cloud Community
hassan121
Contributor
Contributor

VM powered off email notification

i will be thankful if you help me with Power Cli script which will shoot an Email alert if any vm is powered off and the user who initiated this action .

 

0 Kudos
7 Replies
LucD
Leadership
Leadership

Do you mean an Alarm?
What do you already have?


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

0 Kudos
hassan121
Contributor
Contributor

yes . i mean an alarm .Right now i enabled an alarm to send email notification but the received email not clear . i want only vm name and user who powerd off the vm . please find the attached screenshot

0 Kudos
LucD
Leadership
Leadership

And what is the code you used to create that Alarm?


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

0 Kudos
hassan121
Contributor
Contributor

i didn't use powercli code . i configured from vsphere appliance only

0 Kudos
LucD
Leadership
Leadership

The SMTP Alarm action does not give you access to the Username.
Only these fields.

LucD_0-1673772615885.png

 

You could consider running a script to gather that information, compose the email and send it.
But that would require an elaborate setup since you should not run scripts on your VCSA.


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

0 Kudos
hassan121
Contributor
Contributor

okay . i tryed to run this but i got error .

 

$cluster = Get-Cluster -Name "ClusterName"
$vms = Get-VM -Location $cluster
New-AlarmDefinition -Name "Cluster VM Powered Off" -Entity $cluster -TriggerStatus "Red" -TriggerType "SpecificEvent" -TriggerEvent "PoweredOff"
$alarm = Get-AlarmDefinition -Name "Cluster VM Powered Off"
New-Alarm -AlarmDefinition $alarm -Name "Cluster VM Powered Off Alarm"
$email = New-AlarmAction -Email -To "admin@example.com" -Subject "Cluster VM Powered Off" -Body "A VM in the cluster has been powered off."
Set-Alarm -Alarm $alarm -Action $email
Enable-Alarm -Alarm $alarm

 

erorr getting :

 

New-AlarmDefinition : A parameter cannot be found that matches parameter name 'TriggerStatus'.
At line:1 char:54
+ ... armDefinition -Name "" -Entity $cluster -TriggerStatus "Red" - ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-AlarmDefinition], ParameterBindingException
+ FullyQualifiedErrorId : Name

New-Alarm -AlarmDefinition $alarm -Name "Cluster VM Powered Off Alarm"
New-Alarm : The term 'New-Alarm' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1

 

$email = New-AlarmAction -Email -To "" -Subject "Cluster VM Powered Off" -Body "A VM in the cluster has been powered off."

Set-Alarm : The term 'Set-Alarm' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Set-Alarm -Alarm $alarm -Action $email
+ ~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-Alarm:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException

 

 

0 Kudos
LucD
Leadership
Leadership

Try like this.

Note: the Alarm is created "disabled", change as you like

 

$clusterName = 'ClusterName'
$to = 'admin@example.com'

$entity = Get-Cluster -Name $clusterName
$sTrigger = @{
  EventType = Get-EventType | where{$_.Key -eq 'vmPoweredOffEvent'}
  EntityStatus = 'Red'
  EntityType = 'VirtualMachine'
}
$trigger = New-AlarmTrigger @sTrigger
$sAction = @{
  Email = $true
  Subject = 'VM Powered Off'
  To = $to
  Body = '{Target Name} is Powered Off'
}
$action =New-AlarmAction 
$sAlarm = @{
  Name = 'Test VM Powered Off 2'
  Description = 'Send email when a VM is powered off'
  Disabled = $true
  AlarmTrigger = $trigger
  AlarmAction = $action
  Entity = $entity
  Confirm = $false
}
New-AlarmDefinition @sAlarm

 


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

0 Kudos