PoshCode Archive  Artifact [84bd06a664]

Artifact 84bd06a664f74e3947e522e2e92b6d06d2230eb13faad347bfc86b1d99afdaa2:

  • File PWD-Expiration-Email.ps1 — part of check-in [9cc3e07463] at 2018-06-10 13:04:51 on branch trunk — Check to see if users passwords will expire in X days and send them an email notification. This script was written using the Active Directory cmdlets bundled with Server 2008 and Powershell 2.0 (user: St3v3o size: 2608)

# encoding: ascii
# api: powershell
# title: PWD Expiration Email
# description: Check to see if users passwords will expire in X days and send them an email notification.  This script was written using the Active Directory cmdlets bundled with Server 2008 and Powershell 2.0
# version: 0.1
# type: module
# author: St3v3o
# license: CC0
# x-poshcode-id: 2088
# x-archived: 2017-04-30T10:09:05
# x-published: 2011-08-18T07:47:00
#
#
#Active Directory Group Name To Be Edited
#Load Active Directory Module
if(@(get-module | where-object {$_.Name -eq "ActiveDirectory"} ).count -eq 0) {import-module ActiveDirectory}

# get domain maximumPasswordAge value

$MaxPassAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.days

if($MaxPassAge -le 0)

{ 

  throw "Domain 'MaximumPasswordAge' password policy is not configured."

} 

#Send Alert to User

$DaysToExpire = 7

$LogPath = "C:\Scripts\Logs\PasswordExpire"

#Create Daily Log File
$a=get-date -format "ddMMyyyy"
echo "Daily Log for $a" | Out-File $LogPath\$a.txt -append
echo "-----------------------" | Out-File $LogPath\$a.txt -append

#Check users that have a password expiring in 7 days or less

Get-ADUser -SearchBase (Get-ADRootDSE).defaultNamingContext -Filter {(Enabled -eq "True") -and (PasswordNeverExpires -eq "False") -and (mail -like "*")} -Properties * | Select-Object Name,SamAccountName,mail,@{Name="Expires";Expression={ $MaxPassAge - ((Get-Date) - ($_.PasswordLastSet)).days}} | Where-Object {$_.Expires -gt 0 -AND $_.Expires -le $DaysToExpire } | ForEach-Object {

#Send Email to user that password is going to expire

$SMTPserver = "exchange.yourdomain.com"

$from = "noreply@yourdomain.com"

$to = $_.mail

$subject = "Password reminder: Your Windows password will expire in $($_.Expires) days"

$emailbody = "Your Windows password for the account $($_.SamAccountName) will expire in $($_.Expires) days.  For those of you on a Windows machine, please press CTRL-ALT-DEL and click Change Password.  

For all others, you can change it with a web browser by using this link: https://yourdomain.com/owa/?ae=Options&t=ChangePassword

Please remember to also update your password everywhere that might use your credentials like your phone or instant messaging application. 

If you need help changing your password please contact helpdesk@yourdomain.com"


$mailer = new-object Net.Mail.SMTPclient($SMTPserver)

$msg = new-object Net.Mail.MailMessage($from, $to, $subject, $emailbody)

$mailer.send($msg) 

echo $($_.mail)  | Out-File $LogPath\$a.txt -append

}