[powershell]
#############################################
#For:检测AD密码过期时间并邮件通知
#Version:1.0
##############################################
Import-Module Activedirectory
#@1和@2选择一个执行
#@1检索出指定OU里不包含设置了永不过期及禁用的账户
#$alladuser=get-aduser -searchbase "OU=test,DC=ceshi,DC=cn" -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(180)
#当前时间
$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">To </span></a><span style="font-size: 12.0pt;
font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">$chineseusername<span
lang="EN-US"> :</span></span></p>
<p class="MsoNormal" align="left" style="text-align: justify; margin: 0cm 0cm 0.0001pt;
font-size: 10.5pt; font-family: Calibri, sans-serif;">
<span style="font-size: 12.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">
&nbsp; &nbsp; 您的域账户(邮件<span lang="EN-US">&amp;RTX</span>)密码即将在<span lang="EN-US"> <font
color="#ff0000">$expire_days</font> </span>天后过期,<span lang="EN-US"> <font color="#ff0000">
$pwdlastday</font> </span>之后您将无法使用该账户登陆相关系统,请您尽快登录账户更改密码。</span></p>
<p class="MsoNormal" align="left" style="text-align: justify; margin: 0cm 0cm 0.0001pt;
font-size: 10.5pt; font-family: Calibri, sans-serif;">
<span style="font-size: 12.0pt; font-family: 宋体; mso-bidi-font-family: 宋体; mso-font-kerning: 0pt">
&nbsp; &nbsp; 重置密码过程请遵循以下原则:</span></p>
<p class="MsoNormal" align="left" style="text-align: justify; margin: 0cm 0cm 0.0001pt;
font-size: 10.5pt; font-family: Calibri, sans-serif;">
<span style="font-family: 宋体; font-size: 16px; line-height: 24px; background-color: window;">
&nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16px; line-height: 24px;
background-color: window;">&nbsp;</span><span style="font-size: 12.0pt; font-family: 宋体;
mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><span lang="EN-US">○</span>密码长度最少<span
lang="EN-US">6</span>位,</span>密码长度最多<span
lang="EN-US">14</span>位;</span></p>
<p class="MsoNormal" align="left" style="text-align: justify; margin: 0cm 0cm 0.0001pt;
font-size: 10.5pt; font-family: Calibri, sans-serif;">
<span style="font-family: 宋体; font-size: 16px; line-height: 24px; background-color: window;">
&nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16px; line-height: 24px;
background-color: window;">&nbsp;</span><span style="font-size: 12.0pt; font-family: 宋体;
mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><span lang="EN-US">○</span>密码可使用最长时间<span
lang="EN-US">180</span>天,过期需要更改密码;</span></p>
<p class="MsoNormal" align="left" style="text-align: justify; margin: 0cm 0cm 0.0001pt;
font-size: 10.5pt; font-family: Calibri, sans-serif;">
<span style="font-family: 宋体; font-size: 16px; line-height: 24px; background-color: window;">
&nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16px; line-height: 24px;
background-color: window;">&nbsp;</span><span style="font-size: 12.0pt; font-family: 宋体;
mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><span lang="EN-US">○</span>密码最短使用<span
lang="EN-US">1</span>天(<span lang="EN-US">1</span>天之内不能再次修改密码);</span></p>
<p class="MsoNormal" align="left" style="text-align: justify; margin: 0cm 0cm 0.0001pt;
font-size: 10.5pt; font-family: Calibri, sans-serif;">
<span style="font-family: 宋体; font-size: 16px; line-height: 24px; background-color: window;">
&nbsp; &nbsp;</span><span style="font-family: 宋体; font-size: 16px; line-height: 24px;
background-color: window;">&nbsp;</span><span style="font-size: 12.0pt; font-family: 宋体;
mso-bidi-font-family: 宋体; mso-font-kerning: 0pt"><span lang="EN-US">○</span>强制密码历史<span
lang="EN-US">3</span>个(不能使用之前最近使用的<span lang="EN-US">3</span>个密码);</span></p>
<p class="MsoNormal" align="left" style="text-align: justify; margin: 0cm 0cm 0.0001pt;
font-size: 13px; font-family: Calibri, sans-serif;">
<span style="font-family: 宋体;"><span lang="EN-US"><font color="#969696">***************************</font></span></span></p>
<p class="MsoNormal" align="left" style="text-align: justify; margin: 0cm 0cm 0.0001pt;
font-size: 13px; font-family: Calibri, sans-serif;">
<span style="font-family: 宋体;"><font color="#969696">系统自动提醒<span lang="EN-US">,</span>请不要回复<span
lang="EN-US">!</span>如有任何疑问,请联系网络管理员。</font></span></p>
"@
#SMTP发信验证
$anonUser = "[email protected]"
$anonPass = ConvertTo-SecureString "testpassword" -AsPlainText -Force
$anonCred = New-Object System.Management.Automation.PSCredential($anonUser, $anonPass)
#发送邮件
Send-MailMessage -To "[email protected]" -Subject "您的域账户密码即将过期,请尽快更改" -Bodyashtml $Emailbody -Attachments D:\ADPsasswdToMail\更改域账户密码(含MAIL+RTX)教程.doc -From "[email protected]" -Credential $anonCred -SmtpServer "mail.ceshi.cn" -Port 2552 -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
}
}
$EmailbodyHTML=$userlist|
sort-object 距离密码过期天数 |
ConvertTo-Html |
Out-String
Send-MailMessage -To "[email protected]" -Subject "域账户密码即将过期人员汇总通知" -Bodyashtml $EmailbodyHTML -From "[email protected]" -Credential $anonCred -SmtpServer "mail.ceshi.cn" -Port 2552 -Encoding ([System.Text.Encoding]::UTF8)</pre>
<pre class="prettyprint linenums">[/powershell]
二、域控部署
在域控创建相关帐号,并在任务计划程序中创建以下任务(PS:运行任务的用户只需域控只读权限即可)
运行程序:
- C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
参数:
- –command “D:\ADPsasswdToMail\ADPsasswdToMail.ps1”
三、脚本下载
[fanctdl filename=’PowerShell检测AD域用户密码到期邮件提醒脚本’ filesize=’8KB’ filedate=’20160606′ href=’http://pan.baidu.com/s/1c192o7m’ filedown=’ADPsasswdToMail’]这里填写的文件的辅助下载链接,A标签即可![/fanctdl]