I wanted to share a little script that has changed the world at my current company. It’s a simple password email notification script. What it does is grabs the users’ whos passwords about to expire. Then it emails that user with instructions on how to reset their password. Then it sends an email to the user’s manager with a list of when the user’s password will expire. Then finally it sends an email to another person with a complete list of all expiring users and when they will expire. After the user hits zero, the script no longer cares about that person. This is to prevent lots of headaches in the long run with on leave and whatnots. The best thing to do is to set up a scheduled task on a server with rsats. The computer will also need a relay setup on the exchange server.
The Script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | param ( [ parameter ( Helpmessage = "Return Email" , Mandatory = $true )] [string] $ReturnEmail , [ parameter ( Helpmessage = "Email Sent to IT" , Mandatory = $true )] [string] $ITEmail , [ parameter ( Helpmessage = "Mail Server" , Mandatory = $true )] [string] $MailServer , [ parameter ( Helpmessage = "Employee Organizational Unit" , Mandatory = $true )] [string] $EmployeeOU , [ parameter ( Helpmessage = "Training Link" , Mandatory = $true )] [string] $TrainingLink , [ parameter ( Helpmessage = "Training Link" , Mandatory = $true )] [string] $DaysWithinExpiration ) $MaxPwdAge = ( Get-ADDefaultDomainPasswordPolicy ).MaxPasswordAge.Days $expiredDate = ( Get-Date ).addDays(- $MaxPwdAge ) $emailDate = ( Get-Date ).addDays( - ( $MaxPwdAge - $DaysWithinExpiration )) $ExpiredUsers = Get-ADUser -Filter { (PasswordLastSet -lt $emailDate ) -and (PasswordLastSet -gt $expiredDate ) -and (PasswordNeverExpires -eq $false ) -and (Enabled -eq $true ) } -Properties DisplayName, PasswordNeverExpires, Manager, PasswordLastSet, Mail, "msDS-UserPasswordExpiryTimeComputed" -SearchBase "$EmployeeOU" | Select-Object DisplayName, samaccountname, manager, PasswordLastSet, @{name = "DaysUntilExpired" ; Expression = { $_ .PasswordLastSet - $ExpiredDate | Select-Object -ExpandProperty Days } }, @{name = "EmailAddress" ; Expression = { $_ .mail } }, @{Name = "ExpiryDate" ; Expression = { [datetime] ::FromFileTime( $_ . "msDS-UserPasswordExpiryTimeComputed" ) } } | Sort-Object PasswordLastSet $ExpiredUsers foreach ( $ExpiredUser in $ExpiredUsers ) { $ReturnHTML = @ " <html> <head> <style> body { Color: #252525; font-family: Verdana,Arial; font-size:11pt; } h1 { text-align: center; color:#C8102E; Font-size: 34pt; font-family: Verdana, Arial; } h2 { text-align: center; color:#9EA2A2; Font-size: 20pt; } h3 { text-align: center; color:#211b1c; Font-size: 15pt; } h4 { text-align: center; color:#242526; Font-size: 15pt; } a:link { color:#C8102E; text-decoration: underline; cursor: auto; font-weight: 700; } a:visited { color:#C8102E; text-decoration: underline; cursor: auto; font-weight: 700; } </style> </head> <body> Dear $($ExpiredUser.DisplayName),<br><br> Your password was last set on <i>$($ExpiredUser.PasswordLastSet)</i>. This means your password will expire in <b>$($ExpiredUser.DaysUntilExpired) Days</b>. Please reset your password before <b>$($ExpiredUser.ExpiryDate)</b><br> <ol> <li>Find a computer connected to the network</li> <li>if the computer is logged in, on your keyboard press, <b>ctrl+alt+del</b> at the same time. </li> <li>Select <b>Change Password</b></li> <li>replace the username section with <b>$($ExpiredUser.samaccountname)</b>.</li> <li>Enter your old password in the old password location.</li> <li>Enter a new password in the new password location.</li> <li>Confirm your new password in the confirm password location</li> <li>Click Enter</li> </ol> <br> <hr> <br> For more Information on how to change your password Please watch these videos.<br> <a href='$TrainingLink'>How to change your Password.</a><br><br> Thank you<br> IT Department </body> </html> " @ Send-MailMessage -To $( $ExpiredUser .EmailAddress) -From "$ReturnEmail" -Subject "$($ExpiredUser.DisplayName) Password Notification" -BodyAsHtml $ReturnHTML -SmtpServer $MailServer } $Managers = $ExpiredUsers | Select-Object -ExpandProperty manager -Unique foreach ( $Manager in $Managers ) { $ReportingtoHTML = $ExpiredUsers | Where-Object { $_ .manager -eq $Manager } | Select-Object -Property DisplayName, @{name = "EmailAddress" ; Expression = { $_ .mail } }, @{label = "DaysUntilExpired" ; expression = { "$($_.DaysUntilExpired) Days" } }, ExpiryDate | ConvertTo-Html -Fragment -As Table $Manager = Get-ADUser $Manager -Properties DisplayName, PasswordNeverExpires, Manager, PasswordLastSet, "msDS-UserPasswordExpiryTimeComputed" | Select-Object DisplayName, samaccountname, mail, manager, PasswordLastSet, @{name = "DaysUntilExpired" ; Expression = { $_ .PasswordLastSet - $ExpiredDate | Select-Object -ExpandProperty Days } }, @{name = "EmailAddress" ; Expression = { $_ .mail } }, @{Name = "ExpiryDate" ; Expression = { [datetime] ::FromFileTime( $_ . "msDS-UserPasswordExpiryTimeComputed" ) } } | Sort-Object PasswordLastSet $ManagerReturnHTML = @ " <html> <head> <style> h1 { text-align: center; color:#C8102E; Font-size: 34pt; font-family: Verdana, Arial; } h2 { text-align: center; color:#9EA2A2; Font-size: 20pt; } h3 { text-align: center; color:#211b1c; Font-size: 15pt; } h4 { text-align: center; color:#242526; Font-size: 15pt; } a:link { color:#C8102E; text-decoration: underline; cursor: auto; font-weight: 700; } a:visited { color:#C8102E; text-decoration: underline; cursor: auto; font-weight: 700; } body { width:75%; background-color:#ffffff;} table {border-collapse: collapse; border: 1px solid rgb(45,41,38); text-align: Center;} th {background-color: #f7a1af; text-align: Center;} tr:nth-child(even) {background-color: #f2f2f2;} tr {text-align: Center;} td {border:1px solid rgb(45,41,38);text-align: Center;} </style> </head> <body style=" font-family:verdana;font-size:13 "> Dear $($Manager.DisplayName),<br><br> Here is a list of all employees that are reporting to you whose passwords are to expire within the next 14 days. Please have these employees reset their passwords <b>before</b> their expirydate. Please note that we have also sent an email to the employee with the instructions on how to reset their password.<br> <br> $ReportingtoHTML <br> The below videos have been sent to these employees. If the employee needs help, refer them to these videos or instruct them how to reset their passwords.<br> <a href='$TrainingLink'>How to change your Password.</a><br><br> <br> Thank you<br> IT Department </body> </html> " @ Send-MailMessage -To $( $Manager .EmailAddress) -From "$ReturnEmail" -Subject "User Password Notification" -BodyAsHtml $ManagerReturnHTML -SmtpServer $MailServer } $ITReturnHTML = $ExpiredUsers | Select-Object @{label = "Name" ; expression = { $_ .Displayname } }, @{Label = "UserName" ; expression = { $_ .samaccountname } }, EmailAddress, @{label = "Manager" ; expression = { $(( $_ .Manager.Split( ',' )).split( '=' )[1]) } }, ExpiryDate, @{Label = "Days" ; expression = { $_ .DaysUntilExpired } } | ConvertTo-Html -Fragment -As Table $ItReportReturnHTML = @ " <html> <head> <style> h1 { text-align: center; color:#C8102E; Font-size: 34pt; font-family: Verdana, Arial; } h2 { text-align: center; color:#9EA2A2; Font-size: 20pt; } h3 { text-align: center; color:#211b1c; Font-size: 15pt; } h4 { text-align: center; color:#242526; Font-size: 15pt; } a:link { color:#C8102E; text-decoration: underline; cursor: auto; font-weight: 700; } a:visited { color:#C8102E; text-decoration: underline; cursor: auto; font-weight: 700; } body { background-color:#ffffff;} table {border-collapse: collapse; border: 1px solid rgb(45,41,38); text-align: Center;} th {background-color: #54c6ff; text-align: Center;} tr:nth-child(even) {background-color: #f2f2f2;} tr {text-align: Center;width:20%;} td {border:1px solid rgb(45,41,38);text-align: Center;} </style> </head> <body style=" font-family:verdana;font-size:13 "> Dear IT,<br><br> Please see the expiring/expired password list below. <br><br> $ITReturnHTML <br> The below videos have been sent to these employees. If the employee needs help, refer them to these videos or instruct them how to reset their passwords.<br> <a href='$TrainingLink'>How to change your Password.</a><br><br> <br> Thank you<br> IT Department </body> </html> " @ Send-MailMessage -To $ITEmail -From "$ReturnEmail" -Subject "Password Expiry Notification" -BodyAsHtml $ItReportReturnHTML -SmtpServer $MailServer $Admins = Get-ADUser -Filter { (samaccountname -like "*_adm" ) -and (PasswordLastSet -lt $emailDate ) -and (PasswordLastSet -gt $expiredDate ) -and (PasswordNeverExpires -eq $false ) -and (enabled -eq $true ) } -Properties * Foreach ( $Admin in $Admins ) { $Username = $Admin .samaccountname -replace "(_.*)" , " " $User = Get-ADUser -Identity $Username -Properties * $ReturnHTML = @" <html> <head> <style> body { Color: #252525; font-family: Verdana,Arial; font-size:11pt; } h1 { text-align: center; color: #C8102E; Font-size: 34pt; font-family: Verdana, Arial; } h2 { text-align: center; color: #9EA2A2; Font-size: 20pt; } h3 { text-align: center; color: #211b1c; Font-size: 15pt; } h4 { text-align: center; color: #242526; Font-size: 15pt; } a:link { color: #C8102E; text-decoration: underline; cursor: auto; font-weight: 700; } a:visited { color: #C8102E; text-decoration: underline; cursor: auto; font-weight: 700; } </style> </head> <body> Dear $( $User .DisplayName),<br><br> Your Admin password was last set on <i>$( $Admin .PasswordLastSet)</i>. This means your password will expire in <b>$( $admin .DaysUntilExpired) Days</b>. Please reset your password before <b>$( $Admin .ExpiryDate)</b><br> <ol> <li>Find a computer connected to the network</li> <li> if the computer is logged in , on your keyboard press, <b>ctrl+alt+del</b> at the same time. </li> <li>Select <b>Change Password</b></li> <li>replace the username section with <b>$( $Admin .samaccountname)</b>.</li> <li>Enter your old password in the old password location.</li> <li>Enter a new password in the new password location.</li> <li>Confirm your new password in the confirm password location</li> <li>Click Enter</li> </ol> <br> <hr> <br> For more Information on how to change your password Please watch these videos.<br> <a href= '$TrainingLink' >How to change your Password.</a><br><br> Thank you<br> IT Department </body> </html> "@ Send-MailMessage -To $($User.EmailAddress) -From " $ReturnEmail " -Subject " $( $User .DisplayName) Password Notification" -BodyAsHtml $ReturnHTML -SmtpServer $MailServer } |