【转载】获取AD中即将过期账号并邮件通知

背景:
公司搭建了AD和Exchange进行配套使用,但是对于邮箱账户密码过期没有一个很好地通知方式。为了解决这个情况,想到获取AD中的即将过期并进行邮件通知。
环境:
Windows Server 2012 R2 、Exchange 2013
Powershell脚本:

############################################
#Author:Young,时间:2017年9月
#Email:Mr_wang_yang@163.com
#For:检测AD密码过期时间并邮件通知
#Version:1.1
##############################################
Import-Module Activedirectory
#@1和@2选择一个执行
#@1检索出指定OU里不包含设置了永不过期及禁用的账户
$alladuser=get-aduser -searchbase “OU=test,DC=bigyoung,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(90)
#当前时间
$now=get-date
#判断账户是否设置了永不过期
$neverexpire=get-aduser $user -Properties * | %{$_.PasswordNeverExpires}
#距离密码过期的时间
$expire_days=($pwdlastday – $now).Days
#判断过期时间天小于7天的账户
if($expire_days -lt 7 -and $neverexpire -like “false” ){
$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”> <font
color=”#ff0000″>$expire_days</font> </span>天后过期,<span lang=”EN-US”> <font color=”#ff0000″>
$pwdlastday</font> </span>之后您将无法使用该账户登陆相关系统,请您尽快在bigyoung.cn上登录账户并更改密码。</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></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”>7</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”>90</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>密码循环周期2次</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></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>英文大写字母(A 到 Z)</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>英文小写字母(a 到 z)</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>10 个基本数字(0 到 9)</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></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>如有任何疑问,请联系运维部-Young。</font></span></p>
“@
#SMTP发信验证
$anonUser = “Young@test.cn”
$anonPass = ConvertTo-SecureString “test$123” -AsPlainText -Force
$anonCred = New-Object System.Management.Automation.PSCredential($anonUser, $anonPass)
#发送邮件
Send-MailMessage -To “$user@blog.bigyoung.cn” -Subject “您的邮箱账户密码即将过期,请尽快更改” -Bodyashtml $Emailbody -From “IT_inform@blog.bigyoung.cn” -Credential $anonCred -SmtpServer “mail.bigyoung.cn” -Port 25 -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 “Young@blog.bigyoung.cn” -Subject “域账户密码即将过期人员汇总通知” -Bodyashtml $EmailbodyHTML -From “IT_inform@blog.bigyoung.cn” -Credential $anonCred -SmtpServer “mail.bigyoung.cn” -Port 25 -Encoding ([System.Text.Encoding]::UTF8)

You may also like...

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据