Jun 28 2024 09:22 AM
I need to make the following:
- from a list of domains to be removed from email addresses in a csv file load them.
- for each user compare all his email addresses and if anyone is from the list of domain to be removed, then remove it from the user's email addresses
How can I do that in powershell ?
thanks
Jul 01 2024 02:58 AM
Jul 01 2024 03:54 AM
Jul 01 2024 04:36 AM
SolutionJul 01 2024 06:46 AM
It seems that there's some issue with the "domain filtering part"
to test the script I did the following
$NeedsNew = get-mailbox -identity "email address removed for privacy reasons" -ResultSize Unlimit
foreach ( $user in $NeedsNew ) {
# Split email addresses by comma (assuming they are stored in a comma-separated list in the 'EmailAddresses' field)
$emailAddresses = $user.EmailAddresses -split ","
# Filter out the email addresses that belong to the domains to be removed
$filteredEmailAddresses = $emailAddresses | Where-Object {
$domain = $_.Split("@")[-1]
-not ($Domtoremove -contains $domain)
}
# Update the user's email addresses
$user.EmailAddresses = ($filteredEmailAddresses -join ",")
but received the error
Exception setting "EmailAddresses": "Cannot convert value "smtp:email address removed for privacy reasons ....."
is not a valid SMTP address.""
At line:23 char:3
+ $user.EmailAddresses = ($filteredEmailAddresses -join ",")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Jul 01 2024 07:05 AM
Jul 01 2024 07:05 AM
Jul 01 2024 07:28 AM
Jul 01 2024 07:31 AM
Jul 01 2024 07:40 AM
Jul 01 2024 07:42 AM
Jul 01 2024 11:07 PM
Jul 01 2024 11:12 PM
Jul 02 2024 01:28 AM
Thanks for your continuous support.
I'm facing this issue right now.
In the script I'm testing if the new filtered list of email addresses we created with your code does include the original primary smtp address, which could have been removed if was in the domains to be removed.
In the case the old primary was removed we have to set the new primary being like "oldname@newdefaultdomain"
So I tried the following
$OldPrimary = $mailbox.PrimarySmtpAddress.Address
if($filteredEmailAddresses -notcontains $OldPrimary){
"MIssing primary"
$NewPrimary="SMTP:"+$OldPrimary.Split("@")[0]+"@newdomain.com"
$proxyAddress = New-Object Microsoft.Exchange.Data.SmtpProxyAddress($NewPrimary, $true)
$filteredEmailAddresses.Add($proxyAddress)
}
however i got this error
New-Object : Exception calling ".ctor" with "2" argument(s): "Specified argument was out of the range of valid values.
Parameter name: The address 'SMTP:email address removed for privacy reasons' is not a valid SMTP address."
At D:\Scripts\\Test-AliasModify2-NEW.ps1:42 char:21
+ ... xyAddress = New-Object Microsoft.Exchange.Data.SmtpProxyAddress($NewP ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Method invocation failed because [Microsoft.Exchange.Data.SmtpProxyAddress] does not contain a method named 'Add'.
At D:\Scripts\Test-AliasModify2-NEW.ps1:43 char:5
+ $filteredEmailAddresses.Add($proxyAddress)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Jul 01 2024 04:36 AM
Solution