Password Expiry Email Notification

This script will email a user in the event that their password is due to expire in X number of days.

 
 
 
 
 
4.6 Star
(153)
79,758 times
Add to favorites
Active Directory
8/7/2018
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Negative Values
    2 Posts | Last post May 10, 2019
    • Any way to set this up to ignore users with a negative DaysToExpire value?  It sends emails to those users with a message that just says "Your Password Will Expire" without a date or time frame.
    • Negative values have always been a pain, and im not sure i really fully understand how they come about.
      
      What i think they mean is that a password was set on never expire, and then a password policy was applied and never expire was removed. Which would mean, password last set + max age, is a date in the past. In theory that user should not be able to login because their password has expired.
      
      Or, it means we are not correctly excluding people who do have password never expire set.
      
      So, do you have anyone who has PasswordNeverExpires set as 'True' when you run this command, get-aduser -filter {(Enabled -eq $true) -and (PasswordNeverExpires -eq $false)}
      
      Alternatively to just work around the issue, you could do something like this..
      
      $notifyUsers = $colUsers | where { ($_.DaysToExpire -ge 0 ) -and ($_.DaysToExpire -le $expireInDays)}
  • for already expired account or 0 day
    3 Posts | Last post May 09, 2019
    • Thanks Mr. your script is really great and easy to use.
      I have question, how to add into this script for user which already has password expired or 0 day expiry?
      
      Would appreciate your help so much!
    • Well, in theorey they wouldn't get the notification as they wouldnt be able to log in.
    • Thank for replying sir.
      Actually I am deploying this to AD on-premise with synchronization to Office 365 account.
      User not actually login using this AD account, so we need a notification system to notice them their password is already expired.
      The expired password will not affect Office 365.
      
      Big help if you can advice me how to get expired password as well.
      Like adding interval 0?
      
  • Regarding schedulling...
    2 Posts | Last post May 06, 2019
    • First, great script, works like a charm!
      I have question about scheduling...
      Let's say I schedule it to run daily, with parameter -expireInDays 21 set, but I also add -interval 3,7,14
      If script "catches" user with expiry in 21 days, on some day and if -interval is set, will user be notified only on interval days, or will he also get an email again on the next day, when script runs in its scheduled time again?
      Thanks!
    • Lets say on day 1 a user has 21 days left.
      You run the script daily with the settings you specify.
      
      Day 1, notified.
      Day 2, not notified.
      Day 3 - 7, not notified.
      Day 8, Notified - 14 days remaining.
      Day 9 - 14, not notified.
      Day 15, notified 7 days remaining.
      Day 16, 17, 18 not notified.
      Day 19, notified 3 days remaining.
      Day 19 would be the last notification.
      
      So i would always include 0,1 in the interval.
      
  • -reportto is not working
    2 Posts | Last post April 24, 2019
    • this is my script:
      
      c:\scripts\PasswordChangeNotification.ps1 -smtpServer aspmx.l.google.com -expireInDays 10 -from "IT Support <it_support@XXX.com>" -reportTo itservice@XXX.com -interval 1,2,3,4,5,6,8,10 -Logging -LogPath "C:\Scripts\passwordexpirelog" 
      
      everything is working great except for the -reportTo itservice@XXX.com im not getting emails.
      i want to receive emails as CC when a user get notifications...
    • So you dont get the reports and you also want a CC to the password reminders?
      
      Are the reports going to junk or not received at all? is this a custom domain on gmail?
  • Scheduled Task Interval Failure
    2 Posts | Last post April 18, 2019
    • Robert,
      
      This is working fantastically from powershell ISE but from a scheduled task the interval parameter is failing.  Writing to the logfile, each line finishes with ,"Skipped - Interval".
      
      I'm running powershell.exe with the following argument:
      -file c:\scripts\scheduled\AD-User-Password-Expire-Notification\PasswordChangeNotification.ps1 -smtpServer mail.domain.com -expireInDays 14 -from "Service Desk <no-reply@domain.com>" -Logging -LogPath "c:\scripts\logfiles\Password-reset-reminder" -testing -testRecipient me@domain.com -interval 1,3,5,7,14
      
      Followed advice of an earlier poster I tried wrapping the command with "" and parameters wrapped with ''.  This actually failed to run.
      
      Any ideas what I maybe doing wrong?
      
      Cheers,
      Joe
      
      
      
      
    • lol.  silly error.  changed -file for -command.  all good.  
      Joe
  • Cannot validate argument on parameter 'To'.
    6 Posts | Last post April 13, 2019
    • Hey Robert,  I have configured and tested w/o issue.  I receive my test notifications and all looks great.  When I go to run this live, I am receiving the following error when attempting to send to a user:
      
      Cannot validate argument on parameter 'To'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
      
      No email goes out.  I do not see a To parameter anywhere in the script however.  
      
      Where do I need to look?  or update?
      
      Thanks
      JH
      
    • -to is a parameter in Send-MailMessage, it is set to use $emailAddress.
      
      So, i would guess that one or more of your users does not have an emailaddress stored in Active Directory.
    • I am assuming you are referring to the 'mail' attribute in AD correct?  I show in the report that there are email addresses populated.  Here is one example:
      
      in 5 days.	zografoss	Sheila Zografos	zografoss@sd5.k12.mt.us	9/10/2018 7:06	5	3/9/2019 7:06	Cannot validate argument on parameter 'To'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
      
      The EmailAddress column is showing the correct address for the user. 
      
      
      
    • Are you able to use Send-MailMessage on it's own to that recipient?
    • Yes I am able to send test messages using Send-MailMessage through the relay.
    • Hey Robert.  So I went back and sent a test email from the script w/o any modifications and was able to successfully send.  
      
      So started reviewing each modification from the originally edited script, and line 54 - the $testing string, I had = "Disabled".  I removed the = "Disabled" from this line and retested and was successful in sending the emails!  
      
      Not sure why this line would have an affect but it is working now.  Thanks for your time on this!
  • if no expiring password is found no log is written
    1 Posts | Last post April 11, 2019
    • if no user are found the script thrown this error:
      
      You cannot call a method on a null-valued expression.
      At c:\scripts\PasswordChangeNotification.ps1:228 char:41
      +     $samLabel = $samAccountName.PadRight <<<< ($padVal," ")
          + CategoryInfo          : InvalidOperation: (PadRight:String) [], RuntimeException
          + FullyQualifiedErrorId : InvokeMethodOnNull
      
      Add-Member : Cannot bind argument to parameter 'InputObject' because it is null.
      At c:\scripts\PasswordChangeNotification.ps1:266 char:31
      +             $user | Add-Member <<<<  -MemberType NoteProperty -Name SendMail -Value "OK"
          + CategoryInfo          : InvalidData: (:) [Add-Member], ParameterBindingValidationException
          + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand
      
      Creating Log File
      Log Output: .\11-4-2019-PasswordLog.csv
      Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
      At c:\scripts\PasswordChangeNotification.ps1:296 char:32
      +     $notifiedUsers | Export-CSV <<<<  $logFile
          + CategoryInfo          : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
          + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand
      
      
      
  • Method invocation failed because [System.Int32] does not contain a method named 'split'.
    5 Posts | Last post April 11, 2019
    • Hi there,
      
      I have implemented this script on several servers. On 1 server I see this error in the logfile and the e-mail is not being send.
      
      What is missing or wrong overhere?
      
      Thanks.
    • Any more info? Line number of the error, full error text? OS of the server?
    • I found the reason. The split is this line:
      
         if(($interval.split(",")) -Contains($daysToExpire.toString()))
      
      That isn't part of the script but on some servers the orginal line:
      
         if(($interval) -Contains($daysToExpire))
      
      Won't work because when using more days in the interval parameter you need the spplit. I thing its different per OS.
      
      That solution was posted earlier.
    • And when not using the split, it will not e-mail. It will keep saying in the log in the collum SendMail just an OK, nothing more. And the interval has got 5.
    • I think that depends how you pass the interval array into the script.
      
      What is the full command you use to run the script?
  • SMTP Issue while Testing
    4 Posts | Last post April 09, 2019
    • Hi there,
      
      Having issues getting the e-mail portion to work with g-mail. We will most likely use Office 365 smtp servers but I just wanted to get this working before I show it to my team.
      
      SMTP Server          : smtp.gmail.com
      Expire in Days       : 21
      From                 : address@fakeemail.com
      Logging              : True
      Log Path             : c:\logFiles
      Testing              : True
      Test Recipient       : address1@fakeemail.com
      Report Recipient     : 
      Intervals            : 
      
      Getting this error:
      
      The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. g1sm7204905ywf.0 - gsmtp
      
      
      I know my credentials are right, and I've followed your YT tutorial on creating the smtp.txt file as well as making it a securestring.
      
      Any thoughts?
      
      I've also added the PORT and USESSL portions to test and I'm not getting in my e-mail. Even without the port type, the error above is asking for some STARTTLS command.
      
      Send-Mailmessage -smtpServer $smtpServer -from $from -to $reportTo -subject $reportSubject -body $reportbody -bodyasHTML -priority High -Encoding $textEncoding -Attachments $logFile -Credential $credential Port 587 -UseSsl -ErrorAction Stop 
    • Have a look at the question on December 2nd 2018, that has the solution you need.
    • I only see questions from Dec 11 then it jumps to Nov 29. I'm missing something here. Please help.
    • Sorry don't know what happened, it was December 18th.
      
       [System.Net.ServicePointManager]::SecurityProtocol = 'TLS12' 
      
      You need to set the above prior to the sending the message, so i have added it to 'system settings'..
      
      # System Settings
      $textEncoding = [System.Text.Encoding]::UTF8
      $today = $start
      # Set TLS Version, used with SMTP Authentication
      [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
      # End System Settings
  • Remove top line that is coming up in CSV report being emailed
    4 Posts | Last post April 07, 2019
    • Hello! Thank you so much for creating and maintaining/improving this script. It is used by me and I think it is one amazing script.
      
      One question - The is one line being added at the top of the CSV report which is emailed out by your script. The top of the report looks like this:
      
      #TYPE System.Object							
      UserMessage	UserName	Name	EmailAddress	PasswordSet	DaysToExpire	ExpiresOn	SendMail
      
      
      Is there a way to have the "#TYPE System.Object" removed the report? I located this info which may be of help to you:
      https://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/
      
      Thank you!
      
      Best,
      Chris
      
    • That looks quite convoluted to me.
      
      You can probably make this a bit prettier...
      
      $csvNew = "new-report.csv"
      $oldCSV = Get-Content "report.csv"
      $oldCSV = $oldCSV.trim("#TYPE System.Object")
      $oldCSV = $oldCSV | where { $_ -ne "" }
      New-Item $csvNew -ItemType file -Force
      Add-Content $csvNew $oldCSV
    • Can you tell me where in the latest version of your script I would need to place those lines? Thank you!
    • Right before the line where the report is sent.
31 - 40 of 531 Items