############################################
#Author:wangtingdong
#Email:wangtingdong@doubi.ren
#For:检测AD密码过期时间并邮件通知
#Version:1.0
##############################################
Import-Module Activedirectory
#@1和@2选择一个执行
#@1检索出指定OU里不包含设置了永不过期及禁用的账户
#$alladuser=get-aduser -searchbase "DC=doubioa,DC=ren" -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}
$userlist = @()
#################################################
#检测AD密码过期时间并邮件通知相应账户
##################################################
foreach ($user in $alladuser){
#密码最后一次更改时间
$pwdlastset=Get-ADUser $user -Properties * |ForEach{$_.passwordlastset}
#密码的过期时间
$pwdlastday=($pwdlastset).adddays(90)
#当前时间
$now=get-date
#距离密码过期的时间
$expire_days=($pwdlastday - $now).Days
#判断过期时间天小于7天的账户
if($expire_days -lt 7 ){
$chineseusername= Get-ADUser $user -Properties * |ForEach{$_.Displayname}
#邮件正文
$Emailbody=
@"
<p class="MsoNormal" align="left" style="text-align: justify; margin: 0cm 0cm 0.0001pt;
font-size: 10.5pt; font-family: Calibri, sans-serif;">
<a name="_MailOriginal"><span lang="EN-US" style="font-size: 12.0pt; font-family: 宋体;
mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">您好, </span><span style="font-size: 12.0pt;
font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">$chineseusername<span
lang="EN-US"> :</span></span>
#############################################
#查找账户的密码过期时间并发送至管理员账户
#############################################
$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
}
}
$EmailbodyHTML=$userlist|
sort-object 距离密码过期天数 |
ConvertTo-Html |
Out-String
Send-MailMessage -To "help.list@doubi.ren" -Subject "域账户密码即将过期人员汇总通知" -Bodyashtml $EmailbodyHTML -From "pwdnotice.srv@doubi.ren" -SmtpServer "mail.doubi.ren" -Encoding ([System.Text.Encoding]::UTF8)
Exchange Server 2010下,检测用户密码到期通知提醒脚本
原文地址:http://tingdongwang.blog.51cto.com/1056852/1826728