Script to send email notifications to password expired AD users.

Welcome Forums General PowerShell Q&A Script to send email notifications to password expired AD users.

Viewing 1 reply thread
  • Author
    Posts
    • #242213
      Participant
      Topics: 4
      Replies: 2
      Points: 29
      Rank: Member

      I’m looking for the power shell script which sends email notifications to password expired users based on AD.

      So, we have office 365 sync with on-premise AD for user syncronizations with AAD connector.

      I found this below script but its not working.

      #Import AD Module
       Import-Module ActiveDirectory

      #Create warning dates for future password expiration
      $SevenDayWarnDate = (get-date).adddays(7).ToLongDateString()

      #Email Variables
      $MailSender = ” Password AutoBot <[email protected]>”
      $Subject = ‘FYI – Your account password will expire soon’
      $EmailStub1 = ‘I am a bot and performed this action automatically. I am here to inform you that the password for’
      $EmailStub2 = ‘will expire in’
      $EmailStub3 = ‘days on’
      $EmailStub4 = ‘. Please contact the helpdesk if you need assistance changing your password. DO NOT REPLY TO THIS EMAIL.’
      $SMTPServer = ‘smtp.office365.com’

      #Find accounts that are enabled and have expiring passwords
      $users = Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False -and PasswordLastSet -gt 0 } `</strong>
      <strong> -Properties “Name”, “EmailAddress”, “msDS-UserPasswordExpiryTimeComputed” | Select-Object -Property “Name”, “EmailAddress”, `

       @{Name = “PasswordExpiry”; Expression = {[datetime]::FromFileTime($_.”msDS-UserPasswordExpiryTimeComputed”).tolongdatestring() }}

      #check password expiration date and send email on match
      foreach ($user in $users) {
           if ($user.PasswordExpiry -eq $SevenDayWarnDate) {
               $days = 7
               $EmailBody = $EmailStub1, $user.name, $EmailStub2, $days, $EmailStub3, $SevenDayWarnDate, $EmailStub4 -join ‘ ‘

               Send-MailMessage -To $user.EmailAddress -From $MailSender -SmtpServer $SMTPServer -Subject $Subject -Body $EmailBody
           }
          else {}
       }

      I found no errors when i executed this script in AD to analysis issues. still its not sending emails to password expired users.

      I have only replaced this line in above script : $MailSender = ” Password AutoBot <[email protected]>” with my email ID.

      1.my email has Multi factor authentication for login. does this stopping this to send emails ?

      2.What if i create one email or shared mailbox without MFA and use ?

      3.any changes need to be done in script ?

    • #242288
      Participant
      Topics: 13
      Replies: 1684
      Points: 2,853
      Helping Hand
      Rank: Community Hero

      Ram, you are still new to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      This script is normally scheduled to run, so it would not be recommended to use your credentials and to use a service account. Typically, there is an anonymous SMTP mail relay used in most of these cases for enterprises, but if you do use Microsoft’s relay, you need to provide credentials and other settings outlined here:

      Send Email from PowerShell in Office 365

Viewing 1 reply thread
  • You must be logged in to reply to this topic.