I am trying to use PowerShell and our local SMTP server to send some mail notifications to our users. The script is meant to notify users that their password is about to expire. I built the script in stages and each stage was successful, and to a degree the final stage was successful, but it was doing too<\/em> good of a job.<\/p>\n
The script is built to:<\/p>\n The problem comes in that each user receives two identical emails, and I’m not sure why. Here is the entirety of the script:<\/p>\n Import-Module ActiveDirectory<\/p>\n $currentDate = Get-Date<\/p>\n $enabledUsers = Get-ADUser -Filter {Enabled -eq $true -and (EmailAddress -ne ‘[email protected]’) -and (EmailAddress -ne ‘[email protected]’) -and (EmailAddress -ne ‘[email protected]’)} -Properties SamAccountName, DisplayName, EmailAddress, PasswordLastSet, DistinguishedName<\/p>\n $usersWithEmails = @()<\/p>\n $excludedOU = “OU=External,OU=People,DC=domain,DC=com”<\/p>\n $allowedDomains = “@domain.com<\/span>”, “@domain2.com<\/span>”, “@domain3.com<\/span>”<\/p>\n foreach ($user in $enabledUsers) {<\/p>\n $daysSincePasswordSet = ($currentDate - $user.PasswordLastSet).Days<\/p>\n if ($daysSincePasswordSet -gt 166) { $organizationalUnit = ($user.DistinguishedName -split ‘,’, 2)[1] if ($organizationalUnit -ne $excludedOU) { if ($allowedDomains | Where-Object { $user.EmailAddress -like “$_<\/em>” }) {<\/p>\n $usersWithEmails += $user.EmailAddress Write-Host “List of Filtered Email Addresses for Users with Password Set More Than 166 Days Ago (Excluding specified OU and emails):” $smtpServer = “smtp_server.domain.com” $recipients = $usersWithEmails<\/p>\n $messageSubject = “Password Expiration Email Testing” foreach ($recipient in $recipients) { if ($recipient -ne $null) { I am wondering if there is something in that foreach section, but I tried to modify it and it resulted in no emails being sent.<\/p>","upvoteCount":5,"answerCount":9,"datePublished":"2024-01-25T16:25:49.000Z","author":{"@type":"Person","name":"dustinalbright2","url":"https://community.spiceworks.com/u/dustinalbright2"},"acceptedAnswer":{"@type":"Answer","text":" I believe that I have found the issue. When we ran the prepackaged process to install the CodeTwo as our signature control measure, it created a rule that had several exceptions on it to prevent this type of behavior. I simply added another exception to exclude the subject with the string of text we will use for the notifications and the first test after doing so was a success, no duplication. Turns out the PowerShell script was correct and it was not to blame.<\/p>","upvoteCount":1,"datePublished":"2024-01-25T17:22:58.000Z","url":"https://community.spiceworks.com/t/powershell-script-duplicating-emails/965930/8","author":{"@type":"Person","name":"dustinalbright2","url":"https://community.spiceworks.com/u/dustinalbright2"}},"suggestedAnswer":[{"@type":"Answer","text":" I am trying to use PowerShell and our local SMTP server to send some mail notifications to our users. The script is meant to notify users that their password is about to expire. I built the script in stages and each stage was successful, and to a degree the final stage was successful, but it was doing too<\/em> good of a job.<\/p>\n The script is built to:<\/p>\n The problem comes in that each user receives two identical emails, and I’m not sure why. Here is the entirety of the script:<\/p>\n Import-Module ActiveDirectory<\/p>\n $currentDate = Get-Date<\/p>\n $enabledUsers = Get-ADUser -Filter {Enabled -eq $true -and (EmailAddress -ne ‘[email protected]’) -and (EmailAddress -ne ‘[email protected]’) -and (EmailAddress -ne ‘[email protected]’)} -Properties SamAccountName, DisplayName, EmailAddress, PasswordLastSet, DistinguishedName<\/p>\n $usersWithEmails = @()<\/p>\n $excludedOU = “OU=External,OU=People,DC=domain,DC=com”<\/p>\n $allowedDomains = “@domain.com<\/span>”, “@domain2.com<\/span>”, “@domain3.com<\/span>”<\/p>\n foreach ($user in $enabledUsers) {<\/p>\n $daysSincePasswordSet = ($currentDate - $user.PasswordLastSet).Days<\/p>\n if ($daysSincePasswordSet -gt 166) { $organizationalUnit = ($user.DistinguishedName -split ‘,’, 2)[1] if ($organizationalUnit -ne $excludedOU) { if ($allowedDomains | Where-Object { $user.EmailAddress -like “$_<\/em>” }) {<\/p>\n $usersWithEmails += $user.EmailAddress Write-Host “List of Filtered Email Addresses for Users with Password Set More Than 166 Days Ago (Excluding specified OU and emails):” $smtpServer = “smtp_server.domain.com” $recipients = $usersWithEmails<\/p>\n $messageSubject = “Password Expiration Email Testing” foreach ($recipient in $recipients) { if ($recipient -ne $null) { I am wondering if there is something in that foreach section, but I tried to modify it and it resulted in no emails being sent.<\/p>","upvoteCount":5,"datePublished":"2024-01-25T16:25:49.000Z","url":"https://community.spiceworks.com/t/powershell-script-duplicating-emails/965930/1","author":{"@type":"Person","name":"dustinalbright2","url":"https://community.spiceworks.com/u/dustinalbright2"}},{"@type":"Answer","text":" After your first Foreach loop finishes running, I would check if $usersWithEmails contains duplicate entries. From looking at the code, I’m guessing that some users have multiple email addresses with different domains, and that is probably why one user is receiving multiple emails.<\/p>","upvoteCount":1,"datePublished":"2024-01-25T16:36:52.000Z","url":"https://community.spiceworks.com/t/powershell-script-duplicating-emails/965930/2","author":{"@type":"Person","name":"Evan7191","url":"https://community.spiceworks.com/u/Evan7191"}},{"@type":"Answer","text":" If you post code, please use the ‘Insert Code’ button. Please and thank you!
\n
<\/a>Import the Active Directory module<\/h1>\n
<\/a>Get the current date<\/h1>\n
<\/a>Get all enabled user accounts excluding specific email addresses<\/h1>\n
<\/a>Initialize an array to store filtered email addresses<\/h1>\n
<\/a>Specify excluded Organizational Unit<\/h1>\n
<\/a>Specify allowed domains<\/h1>\n
<\/a>Display the information for each user with $daysSincePasswordSet greater than 166<\/h1>\n
<\/a>Calculate the number of days since the password was last set<\/h1>\n
\nWrite-Host “Username: $($user.SamAccountName)”
\nWrite-Host “Display Name: $($user.DisplayName)”
\nWrite-Host “Email Address: $($user.EmailAddress)”
\nWrite-Host “Password Last Set: $($user.PasswordLastSet)”<\/p>\n<\/a>Extract and display the Organizational Unit (OU) from DistinguishedName<\/h1>\n
\nWrite-Host “Organizational Unit: $organizationalUnit”<\/p>\n<\/a>Check if the user is not in the excluded Organizational Unit<\/h1>\n
\nWrite-Host “Number of Days Since Password Set: $daysSincePasswordSet days”
\nWrite-Host “-------------------------”<\/p>\n<\/a>Check if the email address contains any of the allowed domains<\/h1>\n
<\/a>Add the filtered email address to the array<\/h1>\n
\n}
\n}
\n}
\n}<\/p>\n<\/a>Output the list of filtered email addresses<\/h1>\n
\n$usersWithEmails<\/p>\n<\/a>SMTP Server configuration<\/h1>\n
\n$smtpFrom = “[email protected]<\/a>”<\/p>\n<\/a>Array of recipients<\/h1>\n
<\/a>Email details<\/h1>\n
\n$messageBody = “*** Testing the password expiration notification email, please ignore. I am attempting to troubleshoot to duplicate emails currently. ***”<\/p>\n<\/a>Loop through recipients and send emails with debugging information<\/h1>\n
\nWrite-Host “Processing recipient: $recipient”<\/p>\n
\nWrite-Host “Sending email to $recipient”
\nSend-MailMessage -SmtpServer $smtpServer -From $smtpFrom<\/code>
\n-To $recipient -Subject $messageSubject<\/code>
\n-Body $messageBody -BodyAsHtml<\/code>
\n-Priority High
\n}
\nelse {
\nWrite-Host “Skipping null recipient”
\n}
\n}<\/p>\n\n
<\/a>Import the Active Directory module<\/h1>\n
<\/a>Get the current date<\/h1>\n
<\/a>Get all enabled user accounts excluding specific email addresses<\/h1>\n
<\/a>Initialize an array to store filtered email addresses<\/h1>\n
<\/a>Specify excluded Organizational Unit<\/h1>\n
<\/a>Specify allowed domains<\/h1>\n
<\/a>Display the information for each user with $daysSincePasswordSet greater than 166<\/h1>\n
<\/a>Calculate the number of days since the password was last set<\/h1>\n
\nWrite-Host “Username: $($user.SamAccountName)”
\nWrite-Host “Display Name: $($user.DisplayName)”
\nWrite-Host “Email Address: $($user.EmailAddress)”
\nWrite-Host “Password Last Set: $($user.PasswordLastSet)”<\/p>\n<\/a>Extract and display the Organizational Unit (OU) from DistinguishedName<\/h1>\n
\nWrite-Host “Organizational Unit: $organizationalUnit”<\/p>\n<\/a>Check if the user is not in the excluded Organizational Unit<\/h1>\n
\nWrite-Host “Number of Days Since Password Set: $daysSincePasswordSet days”
\nWrite-Host “-------------------------”<\/p>\n<\/a>Check if the email address contains any of the allowed domains<\/h1>\n
<\/a>Add the filtered email address to the array<\/h1>\n
\n}
\n}
\n}
\n}<\/p>\n<\/a>Output the list of filtered email addresses<\/h1>\n
\n$usersWithEmails<\/p>\n<\/a>SMTP Server configuration<\/h1>\n
\n$smtpFrom = “[email protected]<\/a>”<\/p>\n<\/a>Array of recipients<\/h1>\n
<\/a>Email details<\/h1>\n
\n$messageBody = “*** Testing the password expiration notification email, please ignore. I am attempting to troubleshoot to duplicate emails currently. ***”<\/p>\n<\/a>Loop through recipients and send emails with debugging information<\/h1>\n
\nWrite-Host “Processing recipient: $recipient”<\/p>\n
\nWrite-Host “Sending email to $recipient”
\nSend-MailMessage -SmtpServer $smtpServer -From $smtpFrom<\/code>
\n-To $recipient -Subject $messageSubject<\/code>
\n-Body $messageBody -BodyAsHtml<\/code>
\n-Priority High
\n}
\nelse {
\nWrite-Host “Skipping null recipient”
\n}
\n}<\/p>\n
\n<\/p>\n