获取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>之后您将无法使用该账户登陆相关系统,请您尽快在mail.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; &
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Young文人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值