ee1242
Contributor
Contributor

Script for vSphere 6.7 to search & replace email address used by alert notification

Within my organization's vSphere 6.7 management console I need to find a certain email address (that belongs to an employee who is no longer with the organization) and change this email address to a different email address

There are a lot of alerts configured within vSphere among a lot of different clusters.

So I'm hoping there is a way I can simply search for this email address and then I can display a list of the results of where this email address has been found within the vSphere notifications so I can replace this email address with the email address of a distribution group.

I found and ran this PowerShell script within PowerCLI but received the errors shown below.

Is there anyone who can tell me what needs to be done so I can successfully run this PowerShell script or who can write a PowerShell script that will perform a search and replace within the vSphere alarm notifications?

$oldEmail = mailto:'abc@domain.com'
$newemail = mailto:'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
            }
        }
    }
}

I have modified the $oldEmail address with the email address of the old employee and have modified the $newemail address value with the email address of the new email distribution group that I would like to use as the replacement email address.

I also know that the email address value specified within the $oldEmail address value is currently set to be emailed by one or more of the vSphere notifications since I see this email address within the vSphere event viewer.

However, when I run the script I receive the error messages shown below.

P.S. The domain name I am posting here has been changed to company.com so that I won't reveal the name of the company where I am working.

What needs to be done to fix this so I can run this PowerCLI script to make this email address replacement? If any other information is needed please let me know.

This is the error message I receive when running this script within PowerCLI:

PS C:\support\Scripts> .\vsn.ps1 mailto:msmith@company.com : The term 'mailto:msmith@company.com' 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 C:\support\Scripts\VSN.ps1:1 char:13 + $oldEmail = mailto:'msmith@company.com' +             ~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : ObjectNotFound: (mailto:msmith@company.com:String) [], CommandNotFoundException     + FullyQualifiedErrorId : CommandNotFoundException mailto:ITCPUnotifications@company.com : The term 'mailto:ITCPUnotifications@company.com' 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 C:\support\Scripts\VSN.ps1:2 char:13 + $newemail = mailto:'ITCPUnotifications@company.com' +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : ObjectNotFound: (mailto:ITCPUnotifications@company.com:String) [], CommandNotFoundException     + FullyQualifiedErrorId : CommandNotFoundException New-AlarmAction : Cannot bind argument to parameter 'To' because it is null. At C:\support\Scripts\VSN.ps1:18 char:68 + ...     New-AlarmAction -AlarmDefinition $alarm -Email -To $newTo -Subjec ... +                                                            ~~~~~~     + CategoryInfo          : InvalidData: (:) [New-AlarmAction], ParameterBindingValidationException     + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Comma    nds.Alarm.NewAlarmAction New-AlarmAction : Cannot bind argument to parameter 'To' because it is null. At C:\support\Scripts\VSN.ps1:18 char:68 + ...     New-AlarmAction -AlarmDefinition $alarm -Email -To $newTo -Subjec ... +                                                            ~~~~~~     + CategoryInfo          : InvalidData: (:) [New-AlarmAction], ParameterBindingValidationException     + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Comma    nds.Alarm.NewAlarmAction New-AlarmAction : Cannot bind argument to parameter 'To' because it is null. At C:\support\Scripts\VSN.ps1:18 char:68 + ...     New-AlarmAction -AlarmDefinition $alarm -Email -To $newTo -Subjec ... +                                                            ~~~~~~     + CategoryInfo          : InvalidData: (:) [New-AlarmAction], ParameterBindingValidationException     + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,VMware.VimAutomation.ViCore.Cmdlets.Comma    nds.Alarm.NewAlarmAction

----------------------

Also, my Windows 10 computer is running PowerShell version 5.1.19041.1320 and this version of PowerCLI.

Is this script able to be run with this version of PowerShell and PowerCLI?

PowerCLI Version
----------------
   VMware PowerCLI 12.2.0 build 17538434
---------------
Component Versions
---------------
   VMware Common PowerCLI Component 12.3 build 17838947
   VMware Cis Core PowerCLI Component PowerCLI Component 12.3 build 17839331
   VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 12.3 build 17839688

Labels (3)
0 Kudos
2 Replies
scott28tt
VMware Employee
VMware Employee

Thread reported so moderators know it should be moved to the area for PowerCLI.

 


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
0 Kudos
LucD
Leadership
Leadership

How did you get the 'mailto' in the strings to populate $oldEmail and $newEmail?
That should be something like

$oldEmail = 'abc@domain.com'
$newemail = 'xyz@domain.com'


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

0 Kudos