Score:0

Script to write email into group working intermittently

jp flag

I am running the script below in exchange management shell. The purposed of this is, I want to add a list of emails from an excel file (file has two columns, names and email) to a group. So as you can see, i've imported the excel, added the group and loop for each member in the excel to add to the group.

The problem is, I'm getting "user with email not found" for a large number of names even if they definitely exist. It's probably around 40% found and 60% not found even if I can verify that the email does exist. Wondering if anyone can see an issue in the script I posted below.

$csvPath = "C:\Users\Desktop\emailNames.csv"
# Replace with the name of the distribution group
$groupName = "emailOfficer" 
# Import the CSV file
$members = Import-Csv -Path $csvPath

# Loop through the CSV and add members to the distribution group
foreach ($member in $members) {
    $email = $member.email.Trim()
   $user = Get-Mailbox | Where-Object { $_.PrimarySMTPAddress  -ieq $email.Trim() }
 #  $user = Get-Mailbox | Where-Object { $_.PrimarySMTPAddress -ilike "*$email*" }
 
 Write-Host($user)
    if ($user) {
        Add-DistributionGroupMember -Identity $groupName -Member $user.Identity
        Write-Host "Added $($user.PrimarySMTPAddress ) to $groupName"
    } else {
        Write-Host "User with email $email not found."
    }
}
Jan avatar
ru flag
Jan
Powershell -eq is case insensitive by default, no need to specify -ieq. Also why the need for trim()? Both csv and PrimarySMTPAddress should contain only the mail address anyway. Also on the Get-Mailbox command, on every mailbox, you read all mailboxes in and filter them with Where-Object. Better to use the -Identity or -Filter parameter of Get-Mailbox
cn flag
`60% not found even if I can verify that the email does exist.` Tell us the PowerShell command *from the script* you are manually running for one email address to validate that.
Kael avatar
pl flag
You are looking for PrimarySMTPAddress. If you have applied email address policy to some of the mailboxes before, their PrimarySMTPAddress may have been changed.
Score:2
us flag

Have you made sure all the addresses in the list are the users' default Primary addresses, since any other aliases assigned to the account won't be found when querying $_.PrimarySMTPAddress

One way to check would be to query the "missing" addresses with Get-Recipient since that will find an address whereever it is, like this

Write-Host($user)
if ($user) {
    Add-DistributionGroupMember -Identity $groupName -Member $user.Identity
    Write-Host "Added $($user.PrimarySMTPAddress ) to $groupName"
} else {
    $recipient = Get-Recipient $email -ErrorAction SilentlyContinue
    if ($recipient)
    {
        Write-Host "$email found in $($recipient.Name) - $($recipient.RecipientType)"
    }
    else
    {
        Write-Host "User with email $email not found."
    }
}

with RecipientType showing the type of object that it's assigned to (in case it's not a user mailbox).

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.