VMware Cloud Community
Elliss777
Contributor
Contributor

VMware vCenter 7 Default alarms email recipient update PowerCli Script

I have tried a few PowerCLI scripts that were in VMware support communities for updating the email recipient to a new email address, none of the PowerCli scripts work like the users claim. Some don't work at all while others only update one of the Rules while deleting the email address on a second rule in the alarm. 

Isn't there a good known PowerCli script that will go out and update all the default VMware 7 Alarms with a new recipient email address?

You would think this is something that VMware support already has since the GUI interface is pretty much useless to update all the alarms email recipients manuaully. 

Reply
0 Kudos
21 Replies
LucD
Leadership
Leadership

What code have you tried?


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

Reply
0 Kudos
Elliss777
Contributor
Contributor

Here is one of the scripts that I was pointed to from VMware Support. I have a couple others as well.

 

$oldEmail = 'Virtualization@xxx.com'

$newemail = 'WindowsEngineering@xxx.com'

foreach($alarm in Get-AlarmDefinition){

foreach($action in (Get-AlarmAction -AlarmDefinition $alarm)){

if($action.ActionType -eq 'SendEmail'){

if($action.To.Split(',') -contains $oldEmail){

$newTo = @($action.To.Split(',') | %{

if($_ -eq $oldEmail){

$newemail

}

else{

$_

}

})

}

if($action.To -ne $newTo){

Remove-AlarmAction -AlarmAction $action -Confirm:$false

New-AlarmAction -AlarmDefinition $alarm -Email -To $newTo -Subject $mail.Subject -Confirm:$false

}

}

}

}

Reply
0 Kudos
LucD
Leadership
Leadership

Do the SendMail actions contain multiple email addresses in the To field?


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

Reply
0 Kudos
Elliss777
Contributor
Contributor

Yes, they do. 

Reply
0 Kudos
LucD
Leadership
Leadership

Since you got that code probably from Edit Alarm Action E-mail Recipients - VMware Technology Network VMTN
I assume you noticed that there might be an issue with the way PowerCLI combines multiple email addresses, i.e. with a comma or a semi-column.

Is that the error you are getting on the To parameter?


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

Reply
0 Kudos
Elliss777
Contributor
Contributor

Not really getting an error. Let me run it again and see if there are any errors.

Reply
0 Kudos
Elliss777
Contributor
Contributor

New-AlarmAction : Cannot bind argument to parameter 'To' because it is null.
At C:\Temp\Replace-VMware-Alarm-Email-Recipient-Updated.ps1:35 char:68
+ ... New-AlarmAction -AlarmDefinition $alarm -Email -To $newTo -Subjec ...
+ ~~~~~~
+ CategoryInfo : InvalidData: (:) [New-AlarmAction], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Alarm.NewAlarmAction

If the existing alarms have not existing Email, then it should just ignore trying to set the new email correct?

 

 

Reply
0 Kudos
LucD
Leadership
Leadership

The following line makes sure that only SendEmail actions are handled.

if($action.ActionType -eq 'SendEmail'){

Do you mean that you have SendEmail actions without a value in the To field?


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

Reply
0 Kudos
Elliss777
Contributor
Contributor

No, I was asking why I am seeing the error above about the parameter 'To' because it is Null. and the $newTo seeming to be an invalid option. That is what I am asking you. 

Reply
0 Kudos
LucD
Leadership
Leadership

The $newTo variable seems to be empty, one possible cause could be that there was no To value in the AlarmAction.
Hence the question.

I don't at this time another possible cause to end up with an empty $newTo.

Do you know for which Alarm you get this error?
What exactly is in the SendEmail action fields for that Alarm?


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

Reply
0 Kudos
Elliss777
Contributor
Contributor

Hi, sorry, can I tell from the error which alarm it is erroring on? I do not see it.

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership

No, but you can add some debugging lines in the snippet, which should give your more information where the error occurs.

$oldEmail = 'Virtualization@xxx.com'
$newemail = 'WindowsEngineering@xxx.com'
foreach ($alarm in Get-AlarmDefinition) {
    foreach ($action in (Get-AlarmAction -AlarmDefinition $alarm)) {
        if ($action.ActionType -eq 'SendEmail') {

            Write-Host "Looking at alarm $($alarm.Name)"
            Write-Host "Old email $($action.To)"
            
            if ($action.To.Split(',') -contains $oldEmail) {
                $newTo = @($action.To.Split(',') | % {
                        if ($_ -eq $oldEmail) {
                            $newemail
                        } else {
                            $_
                        }
                    })
            }
            if ($action.To -ne $newTo) {
                Remove-AlarmAction -AlarmAction $action -Confirm:$false
                New-AlarmAction -AlarmDefinition $alarm -Email -To $newTo -Subject $mail.Subject -Confirm:$false
            }
        }
    }
}


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

Reply
0 Kudos
Elliss777
Contributor
Contributor

Thank you, I will try that and then look at the specific alarm to try and get better information for why it is erroring. 

Tags (1)
Reply
0 Kudos
Elliss777
Contributor
Contributor

Odd, I saved that small script you posted as a new version, updated the correct email recipient addresses and ran it. It seems to have run with out any errors. 

Reply
0 Kudos
LucD
Leadership
Leadership

You have 2 options
- use that latest snippet, remove the 2 Write-Host lines, and update your Alarms
- run a comparison between the 2 files and find the differences, besides the 2 Write-Host lines

In any case, glad your issue seems to be solved.


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

Reply
0 Kudos
Elliss777
Contributor
Contributor

OK, I change two rules to have the send email on for Rule 1 and put the old email in. Ran it. it worked to change only the first alarm that was enable. So I just ran it again to see what it would say and below you can see the results. The second time it ran it got an error. The script still has the two lines that were added for debugging.

PS C:\Temp> .\Replace-VMware-Alarm-Email-Recipient-Updated-Bugcheck.ps1
Looking at alarm Host Requires Encryption Mode Enabled Alarm
Old email virtualization@empower-retirement.com

ActionType Trigger
---------- -------
SendEmail ...
Looking at alarm vCenter HA Service Health Alarm.
Old email virtualization@empower-retirement.com
SendEmail ...

 

PS C:\Temp> .\Replace-VMware-Alarm-Email-Recipient-Updated-Bugcheck.ps1
Looking at alarm Host Requires Encryption Mode Enabled Alarm
Old email WindowsEngineering@empower.com
New-AlarmAction : Cannot bind argument to parameter 'To' because it is null.
At C:\Temp\Replace-VMware-Alarm-Email-Recipient-Updated-Bugcheck.ps1:21 char:68
+ ... New-AlarmAction -AlarmDefinition $alarm -Email -To $newTo -Subjec ...
+ ~~~~~~
+ CategoryInfo : InvalidData: (:) [New-AlarmAction], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Alarm.NewAlarmAction

Looking at alarm vCenter HA Service Health Alarm.
Old email WindowsEngineering@empower.com
New-AlarmAction : Cannot bind argument to parameter 'To' because it is null.
At C:\Temp\Replace-VMware-Alarm-Email-Recipient-Updated-Bugcheck.ps1:21 char:68
+ ... New-AlarmAction -AlarmDefinition $alarm -Email -To $newTo -Subjec ...
+ ~~~~~~
+ CategoryInfo : InvalidData: (:) [New-AlarmAction], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Alarm.NewAlarmAction

 

Reply
0 Kudos
Elliss777
Contributor
Contributor

The script actually did not update the email, but it disabled the Rule 1 alarm for and turned off the Send email notifications. So this script does not do anything that we are expecting it to do.

Elliss777_0-1679957342049.png

 

Reply
0 Kudos
LucD
Leadership
Leadership

It looks as if the issue mentioned in KB2150106 is still there.
Meaning I had to drop the PowerCLI cmdlets and switch to calling the API methods directly.

Can you give the following a try?

$oldEmail = 'Virtualization@xxx.com'
$newEmail = 'WindowsEngineering@xxx.com'

$alarmMgr = Get-View AlarmManager
$root = Get-Folder -Name Datacenters
Get-View -Id ($alarmMgr.GetAlarm($root.Id)) -PipelineVariable alarm |
ForEach-Object -Process {
  $spec = [VMware.Vim.AlarmSpec]($alarm.Info)
  $spec.Action.Action | Where-Object { $_.Action -is [VMware.Vim.SendEmailAction] } |
  ForEach-Object -Process {
    $toMail = $_.Action.ToList.Split(',')
    if($toMail -contains $oldEmail){
      $_.Action.ToList = ($toMail.Replace($oldEmail,$newEmail) | Sort-Object -Unique) -join ','
    }
  }
  if($toMail -contains $oldEmail){
    Write-Host "Updated Alarm $($alarm.Info.Name)"
    $alarm.ReconfigureAlarm($spec)
  }
}





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

Reply
0 Kudos
Elliss777
Contributor
Contributor

This version had a much better output of all of the alarms, like it did go through all of the alarms, but the few that I had set to have the old email address did not update with the new email. The output looked like it did hit all of the existing alarms! Here is a small sample of the output.

Updated Alarm Host Requires Encryption Mode Enabled Alarm
Updated Alarm Virtual machine memory usage
Updated Alarm vCenter Stats Monitor Service Health Alarm.
Updated Alarm vCenter HA Service Health Alarm.
Updated Alarm PSC Service Health Alarm.
Updated Alarm Image Builder Service Health Alarm
Updated Alarm Update Manager Service Health Alarm
Updated Alarm vMon API Service Health Alarm
Updated Alarm Component Manager Service Health Alarm
Updated Alarm VMware vSphere Authentication Proxy Service Health Alarm
Updated Alarm vSAN Health Service Alarm
Updated Alarm VMware vCenter-Services Health Alarm
Updated Alarm Datastore usage on disk
Updated Alarm License inventory monitoring
Updated Alarm Host hardware sensor state
Updated Alarm vCenter HA Cluster Health Alarm
Updated Alarm VMware Directory Service Health Alarm
Updated Alarm vSAN network alarm 'Hosts disconnected from VC'
Updated Alarm vSAN stretched cluster alarm 'Unicast agent not configured'
Updated Alarm vSAN network alarm 'Hosts with connectivity issues'
Updated Alarm vSAN health service alarm for Overall Health Summary
Updated Alarm Timed out starting Secondary VM
Updated Alarm vSAN network alarm 'All hosts have a vSAN vmknic configured'
Updated Alarm Virtual machine encryption integrity check failed
Updated Alarm Identity Provider Users and Groups token is about to expire
Updated Alarm Host Memory Mode High Active DRAM Usage
Updated Alarm Virtual Machine High PMem Bandwidth Usage
Updated Alarm Lifecycle Disk Exhaustion on APPVCSA01T
Updated Alarm LVM Snapshot Disk Exhaustion on APPVCSA01T
Updated Alarm vStats DB Disk Exhaustion on APPVCSA01T

 

Reply
0 Kudos