############################################
#Author:wangtingdong
#For:检测AD密码过期时间并邮件通知
#Version:1.0
##############################################
Import-Module Activedirectory
#@1和@2选择一个执行
#@1检索出指定OU里不包含设置了永不过期及禁用的账户
#$alladuser=get-aduser -searchbase "DC=archermind,DC=com" -Filter 'PasswordNeverExpires -eq "false" -and enabled -eq "true"' |ForEach{$_.SamAccountName}
#@2检索出所有OU里不包含设置了永不过期及禁用的账户
#$alladuser=Get-ADUser -Filter 'PasswordNeverExpires -eq "false" -and enabled -eq "true"' |ForEach{$_.SamAccountName}
$alladuser=get-aduser -filter * -properties Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, EmailAddress |where {$_.Enabled -eq "True"} | where { $_.PasswordNeverExpires -eq $false } | where { $_.passwordexpired -eq $false }
#$emailaddress = $alladuser.emailaddress
$userlist = @()
#################################################
#检测AD密码过期时间并邮件通知相应账户
##################################################
foreach ($user in $alladuser){
#密码最后一次更改时间
$pwdlastset=Get-ADUser $user -Properties * |ForEach{$_.passwordlastset}
#获取邮件地址
$useremail=Get-ADUser $user -Properties * |ForEach{$_.emailaddress}
#密码的过期时间
$pwdlastday=($pwdlastset).adddays(90)
#当前时间
$now=get-date
#距离密码过期的时间
$expire_days=($pwdlastday - $now).Days
#判断过期时间天小于7天的账户
if($expire_days -lt 7 ){
$chineseusername= Get-ADUser $user -Properties * |ForEach{$_.name}
#邮件正文
$Emailbody=
@"
您的邮箱账户密码即将在 color="#ff0000">$expire_days 天后过期,
$pwdlastday 之后您将无法使用该账户登陆相关系统,请您尽快登录 https://webmail.archermind.com 更改密码,选项--常规--我的账户--更改帐号密码。
重置密码过程请遵循以下原则:
1、密码长度最少 >8位,密码中不能包含帐号信息;
2、密码可使用最长时间 >90天,过期需要更改密码;
3、密码设置中必须包含:英文大写字母(A-Z),英文小字字母(a-z),10个基本数字(0-9),特殊字符(如!$ % ^ *等)四种符号中的三种。
4、强制密码历史 >3个(不能使用之前最近使用的3个密码);
Your email account password will expire in color="#ff0000">$expire_days days after ,
$pwdlastday you will not be able to use the account login system, please login https://webmail.archermind.com to change the password as soon as possible, options - General - account - my account password change.
Reset password process follow the following principles:
1、 password length of at least > 8 position,the password can not include account information;
2、the password can be used for a maximum of >90days,expired need to change the password;
3、 The password must contain 3 English: uppercase letters, lowercase letters English (A-Z) (A-Z), 10 basic digital (0-9), special characters (such as! Three of the four symbols of the $$^ ^ *, etc..
4、forced password history of the > 3(can not be used before the recent use of the password 3);
************************************************************************************************************************************************************************
系统自动提醒,请不要回复 >!如有任何疑问,请联系[email protected],IT支持电话:025-51887700-888082
"@
#SMTP发信验证
$anonUser = "[email protected]"
$anonPass = ConvertTo-SecureString "ArcherMai209#man" -AsPlainText -Force
sleep 3
#$username=Get-ADUser $user -Properties *
#$userobject=New-object psobject
# $userobject | Add-Member -membertype noteproperty -Name 邮箱 -Value $username.mail
#$emailaddress = $user.emailaddress
#发送邮件
#Send-MailMessage -To $useremail -Subject "您的域账户密码即将过期,请尽快更改!" -Bodyashtml $Emailbody -From "[email protected]" -SmtpServer "smtp.archermind.com" -Encoding ([System.Text.Encoding]::UTF8)
# Send Email Message
Send-Mailmessage -smtpServer "smtp.archermind.com" -from "[email protected]" -to $useremail -subject "您的域账户密码即将过期,请尽快更改!" -body $Emailbody -bodyasHTML -priority High -Encoding ([System.Text.Encoding]::UTF8)
#Send-Mailmessage -smtpServer "smtp.archermind.com" -from "[email protected]" -to "[email protected]" -subject "您的域账户密码即将过期,请尽快更改!" -body $Emailbody -bodyasHTML -priority High -Encoding ([System.Text.Encoding]::UTF8)
#############################################
#查找账户的密码过期时间并发送至管理员账户
#############################################
#$username=Get-ADUser $user -Properties *
#$userobject=New-object psobject
#$userobject | Add-Member -membertype noteproperty -Name 用户名 -value $username.displayname
#$userobject | Add-Member -membertype noteproperty -Name 邮箱 -Value $username.mail
#$userobject | Add-Member -membertype noteproperty -Name 最后一次密码设置 -Value $username.Passwordlastset
#$userobject | Add-Member -membertype noteproperty -Name 密码过期时间 -Value $pwdlastday
#$userobject | Add-Member -membertype noteproperty -Name 距离密码过期天数 -Value $expire_days
#$userlist+=$userobject
}
}
exit
#$EmailbodyHTML=$userlist|
#sort-object 距离密码过期天数 |
#ConvertTo-Html |
#Out-String
#Send-MailMessage -To "[email protected]" -Subject "域账户密码即将过期人员汇总通知" -Bodyashtml $EmailbodyHTML -From "[email protected]" -SmtpServer "smtp.archermind.com" -Encoding ([System.Text.Encoding]::UTF8)