############################################
#Author:Young,时间:2018年1月 #Email:azhe.911@foxmail.com #For:检测AD快要过期/已经过期的公共账户并邮件通知 #Version:1.3
##############################################
#导入活动目录powershell模块 Import-Module Activedirectory
#当前时间 $TodayDate=Get-Date
#查看指定OU中的所有账户的Samaccountname $Userlist=Get-ADUser -SearchBase "ou=test,dc=outlook,dc=com" -Filter * | %{$_.Samaccountname}
################################################# #循环检测满足Samaccountname,AccountExpirationDate,Manager属性的账户 ################################################## foreach ($user in $Userlist){ $AccountExpirationDat=Get-ADUser $user -Properties Samaccountname,AccountExpirationDate,Manager,Enabled
#距离账户过期的时间天数 $expire_days=($AccountExpirationDat.AccountExpirationDate - $TodayDate).Days #赋值是公邮经理的邮箱地址 $Manager=(Get-ADUser $AccountExpirationDat.Manager).UserPrincipalName
#赋值是公邮经理的名字
$ManagerName=(Get-ADUser $AccountExpirationDat.Manager).Name
#赋值是公邮的邮名称
$NameUser=(Get-ADUser $AccountExpirationDat.Samaccountname).Name
#检索包含有快要账户过期时间的、并有管理人员属性的(这里值得是公邮账户属性中的组织中经理的赋值) if($AccountExpirationDat.AccountExpirationDate -and $AccountExpirationDat.Manager -and $expire_days -ge 0 -and $expire_days -le 15 -and $AccountExpirationDat.Enabled -eq "True" ) {
#验证输出的那些账户
#$AccountExpirationDat
#邮件正文
$Emailbady=
"您好, $ManagerName :<p>
您的 $NameUser 公共邮箱账户还有 $expire_days 天后就要过期了。 $expire_days 天后您的账户会被禁用将影响您这个公共邮箱账户的正常使用,请尽快联<br />
<br />
系服务台010-10100120申请您的公共邮箱继续使用权限。 <br />
<br />
<br />
<font size=8: color=green><b>北京xxxx经纪有限公司</b></font><br />
<font size=8: color=green><b>IT Department</b></font></p> "
# 是代表空格<font size=8: color=green><b>IT Department</b></font>这个是调节字体大小和颜色<b>IT Department</b>是将字体加粗
#发送邮件给公邮管理人员(经理)邮箱通知
Send-MailMessage -From "zhangsan@outlook.com" -to "$manager" -Subject "您的公共邮箱账户即将过期" -BodyAsHtml $Emailbady -SmtpServer mail.outlook.com -Encoding ([System.Text.Encoding]::UTF8)
}
#检索已经过期的公共邮箱账户发送邮件通知 elseif($AccountExpirationDat.AccountExpirationDate -and $AccountExpirationDat.Manager -and $expire_days -lt 0 -and $AccountExpirationDat.Enabled -eq "True" ) {
#验证输出的那些账户
#$AccountExpirationDat
#邮件正文
$Emailbady=
"您好, $ManagerName :<p>
您的 $NameUser 公共邮箱账户已经过期 $expire_days 天了。 您的公共邮箱账户已经禁用请确认一下是否还需要使用,请尽快联系服务台010-<br />
<br/>
110101010如果需要继续使用请提交一个继续使用的申请,如果该公共邮箱不再使用请提交一个删除账户的申请。 <br />
<br />
<br />
<font size=8: color=green><b>北京xxxx经纪有限公司</b></font><br />
<font size=8: color=green><b>IT Department</b></font></p> "
# 是代表空格<font size=8: color=green><b>IT Department</b></font>这个是调节字体大小和颜色<b>IT Department</b>是将字体加粗
#发送邮件给公邮管理人员(经理)邮箱通知
Send-MailMessage -From "zhangsan@outlook.com" -to "$manager" -Subject "您的公共邮箱账户已经过期" -BodyAsHtml $Emailbady -SmtpServer mail.outlook.com -Encoding ([System.Text.Encoding]::UTF8)
}
}