Windows server 2012 R2 AD域密碼過期郵件提醒

最近接領導指示,說要做一個域密碼提醒的服務,這樣會更人性化些,員工也就不必等過期了來找管理員重置。但是但是本人不懂腳本,就乾脆從網上找了些鏈接做爲參考,這裏感謝此鏈接的作者 https://www.yeboyzq.com/windowsserver/632.html

以下是最終的過程

Import-Module Activedirectory

$alladuser=get-aduser -searchbase "ou=,ou=,dc=*,dc=com" -Filter 'PasswordNeverExpires -eq "false" -and enabled -eq "true"' |ForEach{$_.SamAccountName}

$userlist = @()

$itmag = "@.com"

function sendmail($mailaddr,$body) #定義發送郵件的方法
{
$msg=New-Object System.Net.Mail.MailMessage
$msg.To.Add($mailaddr)
#$msg.Bcc.Add($itmag)
$msg.From = New-Object System.Net.Mail.MailAddress("@.com", "@.com",[system.Text.Encoding]::GetEncoding("UTF-8"))
$msg.Subject = "域賬戶密碼即將過期提醒!"
$msg.SubjectEncoding = [system.Text.Encoding]::GetEncoding("UTF-8")
$msg.Body =$body
$msg.BodyEncoding = [system.Text.Encoding]::GetEncoding("UTF-8")
$msg.IsBodyHtml = $false
#$msg.Priority = [System.Net.Mail.MailPriority]::High
$client = New-Object System.Net.Mail.SmtpClient("smtp.office365.com")
$client.Port = 587
$client.EnableSsl = $true
$client.UseDefaultCredentials = $false
$client.Credentials=New-Object System.Net.NetworkCredential("@.com", "password")

try {$client.Send($msg)}  
catch [Exception]
{$($_.Exception.Message)  
$mailaddr  
}

}

foreach ($user in $alladuser)
{
$pwdlastset= Get-ADUser $user -Properties | %{$_.passwordlastset}
$pwdlastday=$pwdlastset.AddDays(90)
$now=get-date
$expire_days=($pwdlastday - $now).Days
$chineseusername= Get-ADUser $user -Properties
| %{$_.Displayname}
$pwdset = $pwdlastset.ToString('yyyy年MM月dd日 HH:mm:ss')
$pwdlast = $pwdlastset.AddDays(90).ToString('yyyy年MM月dd日 HH:mm:ss')
$tomailaddr = $user + "@huobi.com"
if($expire_days -lt 8 -and $expire_days -ge 0 )
{
#郵件正文
$Emailbody=
"Dear $chineseusername :
您的域賬戶密碼即將在 $expire_days 天后過期 ,請您立即更改。
過期時間段參考:
上次密碼設置時間: $pwdset
本次密碼到期時間: $pwdlast

    此密碼爲有線聯網、無線聯網、×××、加域計算機的使用密碼。

    更改密碼請遵循以下原則:
    ○密碼長度最少 10 位;
    ○強制密碼歷史 3個(不能使用之前最近使用的 3 個密碼);
    ○密碼可使用最長時間90天,90天以內可根據鏈接自行更改,超過90天請聯繫IT更改;
          北京IT:****   海南IT:****   深圳IT:****

    ○密碼符合複雜性需求(大寫字母、小寫字母、數字和符號四種中必須有三種、且密碼口令中不得包括全部或部分用戶名)
    ○windows 更改:鍵盤按  Ctrl+Alt+Del
    ○MACbook更改請訪問: 北京: http://
                            海南: http://
                            深圳:http://
    "        
sendmail $tomailaddr $Emailbody   
}
if($expire_days -lt 0){
     $Emailbody=
    "Dear $chineseusername :
    您的域賬戶密碼已過期 ,請聯繫IT人員進行更改。
                上次密碼設置時間: $pwdset
                本次密碼到期時間: $pwdlast
   北京IT:****   海南IT:****   深圳IT:****
    "
sendmail $tomailaddr $Emailbody
}

}

最終郵件效果
即將過期
Windows server 2012  R2   AD域密碼過期郵件提醒
已過期
Windows server 2012  R2   AD域密碼過期郵件提醒

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章