############################################
#Author:wangtingdong
#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=
@"
您好, $chineseusername
lang="EN-US"> :
? ? 您的郵箱賬戶密碼即將在
color="#ff0000">$expire_days
天后過期,$pwdlastday 之后您將無法使用該賬戶登陸相關系統,請您盡快登錄 http://mail.doubi.ren 更改帳號密碼。
? ? 重置密碼過程請遵循以下原則:
? ??1、密碼長度最少
lang="EN-US">8
位,密碼中不能包含帳號信息;? ??2、密碼可使用最長時間
lang="EN-US">90
天,過期需要更改密碼;? ??3、密碼設置中必須包含:英文大寫字母(A-Z),英文小字字母(a-z),10個基本數字(0-9),特殊字符(如!$ % ^ *等)。
? ??4、強制密碼歷史
lang="EN-US">24
個(不能使用之前最近使用的24個密碼);? ??密碼修改方法請登錄:http://wiki.doubioa.ren.com/changepassword
************************************************************************************************************************************************************************
系統自動提醒,請不要回復
lang="EN-US">!
如有任何疑問,請聯系help.list@doubi.ren,IT支持電話:010-8888888"@
#SMTP發信驗證
$anonUser = pwd@doubioa.ren
$anonPass = ConvertTo-SecureString "89OP:./}+" -AsPlainText -Force
#發送郵件
Send-MailMessage -To "$user@doubi.ren" -Subject "您的域賬戶密碼即將過期,請盡快更改!" -Bodyashtml $Emailbody -From "pwd@doubi.ren"? -SmtpServer "mail.doubi.ren" -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 "help.list@doubi.ren" -Subject "域賬戶密碼即將過期人員匯總通知" -Bodyashtml $EmailbodyHTML -From "pwd@doubi.ren"? -SmtpServer "mail.doubi.ren"? -Encoding ([System.Text.Encoding]::UTF8)