I was wondering if anybody had a script for PowerCLI/vCenter 5.5 to mass edit the e-mail recipients under "Configuration" for 'Send a notification email' under Alarm->Actions in vCenter.
I looked throughout Google but I had only found broken links or scripts that are for version < 5.0
I would ideally like it to over-write the entire list of values but if there is one available that will simply append new recipients to all alarms, I could deal with that as well
Thanks for any help you may provide
Try something like this.
It will look at all Alarms that have a SendEmail action, check the To field and replace it.
If you want this for a limited set of Alarms, you'll have provide a Name value for the Get-AlarmDefinition cmdlet.
PS: you have to remove/add the action because there currently is no Set-AlarmAction cmdlet
$oldEmail = 'abc@domain.com'
$newEmail = 'xyz@domain.com'
foreach ($alarm in Get-AlarmDefinition){
$action = Get-AlarmAction -AlarmDefinition $alarm
$mail = $action | where {$_.ActionType -eq 'SendEmail' -and $_.To -contains $oldEmail}
Remove-AlarmAction -AlarmAction $mail -Confirm:$false
New-AlarmAction -AlarmDefinition $alarm -Email -To $newEmail -Subject $mail.Subject -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you so much for the response - is there a way to edit the script to adjust its behaviour for dealing with multiple email addresses in the configuration? I would normally just need a single address removed from the line.
i.e.
Action Configuration
Send a notification email email@domain.com;email2@domain.com;email3@domain.com
I would like to remove 'email2@domain.com' in my example above
Or would it be faster to outright remove the alarm action altogether and re-create it with the values I want?
i.e.
Action Configuration
Send a notification email email@domain.com;email3@domain.com
Try like this, it replaces the old entry and keeps the others
$oldEmail = 'abc@domain.com'
$newEmail = 'xyz@domain.com'
foreach ($alarm in Get-AlarmDefinition){
$action = Get-AlarmAction -AlarmDefinition $alarm
$mail = $action | where {$_.ActionType -eq 'SendEmail' -and $_.To -contains $oldEmail}
if($mail){
Remove-AlarmAction -AlarmAction $mail -Confirm:$false
$newTo = ($mail.To | where{$_ -ne $oldEmail}),$newEmail
New-AlarmAction -AlarmDefinition $alarm -Email -To $newTo -Subject $mail.Subject -Confirm:$false
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I gave those commands an attempt and I was faced with this error message:
New-AlarmAction : Cannot convert 'System.Object[]' to the type 'System.String'
required by parameter 'To'. Specified method is not supported.
Is it missing something after the -Email in the New-AlarmAction line?
Strange, the To parameter should accept an array of strings.
Give it a try like this
New-AlarmAction -AlarmDefinition $alarm -Email -To ($newTo -join ',') -Subject $mail.Subject -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The error message changed slightly after that attempt:
New-AlarmAction : Cannot bind parameter 'To' to the target. Exception setting
"To": "Invalid email address: System.Object[],"
Strange, just tested again in my lab (vSphere 6.5), and for me the original script is working.
How many email addresses do you have in the original To field?
And which vSphere and PowerCLI version do you have?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Oh that was my mistake, I should have specified; I am running vSphere 5.5 with PowerCLI 5.8
There are 5 e-mail addresses present in the Send E-mail trigger, all separated by a semi-colon
Is it an option to upgrade your PowerCLI (we are currently at 6.5.4)?
That one should support vSPhere 5.5 and up.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I went ahead and updated my PowerCLI version and tried to run the script again. This time there are no errors but the Alarms Actions remained unchanged. I upgraded to PowerCLI 6.5 Release 1 build 4624819
Am I using this script right based on the example below? The way I have interpreted the commands below is that it will remove the previous alarm actions that contain ''email1@address.com;email2@address.com;email3@address.com;email4@address.com'' and replace that Send Email recipients value to 'email1@address.com;email3@address.com;'
Currently I am encountering this issue even if I copy-paste the exact line from the Alarm Trigger to avoid potential spelling errors
$oldEmail = 'email1@address.com;email2@address.com;email3@address.com;email4@address.com'
$newemail = 'email1@address.com;email3@address.com;'
($alarm in Get-AlarmDefinition){
$action = Get-AlarmAction -AlarmDefinition $alarm
$mail = $action | where {$_.ActionType -eq 'SendEmail' -and $_.To -contains $oldEmail}
if($mail){
Remove-AlarmAction -AlarmAction $mail -Confirm:$false
$newTo = ($mail.To | where{$_ -ne $oldEmail}),$newEmail
New-AlarmAction -AlarmDefinition $alarm -Email -To ($newTo -join ',') -Subject $mail.Subject -Confirm:$false
I'm starting to wonder what the separator between multiple email addresses must be.
I have seen ; and , but not sure which one is the correct one.
I'll do some more testing.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok, I did some further testing, the separator between email addresses has to be a comma.
The following will replace the old email address with the new email address in all alarms.
The mail To field can have multiple email addresses, only the old email address will be replaced, the other will be kept.
See if this works
$oldEmail = 'abc@domain.com'
$newemail = 'xyz@domain.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
}
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks so much for looking into this.
If the e-mail addresses are currently separate by semi-colon, will that still allow the script to pick out the old e-mail addresses correctly?
If I wanted to just remove an e-mail address from the To field, could I just leave the New Email variable blank to have the Old Email removed?
You will have to change the Split(',') to Split(';'), but when I used multiple email addresses separated with semi-column, the alarm didn't send out any emails.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I tried the script and it wiped out my entire collection of SendMail triggers across my alarm list:
New-AlarmAction : Cannot bind argument to parameter 'To' because it is an
empty string.
+ New-AlarmAction -AlarmDefinition $alarm -Email -To $newTo
-Subje ...
+ ~~~~~~
+ CategoryInfo : InvalidData: (:) [New-AlarmAction], ParameterBin
dingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAl
lowed,VMware.VimAutomation.ViCore.Cmdlets.Commands.Alarm.NewAlarmAction
I guess this happened because I tried to run the script with a blank value in the '$newemail' variable?
Euh, yes, I think so.
But you have a backup (I hope)?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It was done in a DEV environment so I can go through and re-apply the e-mail addresses manually - all good in the spirit of working out the script. Is there a way to backup the alarms in vCenter 5.5?
I mostly want to get the script functional so I can use it in a Production environment
Try Angel's script in Re: Script to export/import Alarms between VC or synchronize alarms between 2 Virtual Centers
Btw, for testing, you can limit a test run to 1 alarm by specifying a Name parameter on the Get-AlarmDefinition cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It doesn't seem to like the e-mail addresses I have input even if I use the (,) to separate the addresses:
New-AlarmAction : Cannot bind parameter 'To' to the target. Exception setting
"To": "Invalid email address:
email1@address.com,email2@address.com"
+ New-AlarmAction -AlarmDefinition $alarm -Email -To $newTo
-Subje ...
+ ~~~~~~
+ CategoryInfo : WriteError: (:) [New-AlarmAction], ParameterBind
ingException
+ FullyQualifiedErrorId : ParameterBindingFailed,VMware.VimAutomation.ViCo
re.Cmdlets.Commands.Alarm.NewAlarmAction
Even if I manually input the addresses in the SendEmail trigger separated by the (,) it will present itself as separated by (;) when I re-open the alarm after setting it.
Could this be a vCenter 5.5 specific behaviour?