把用户选出来,逐个发邮件提醒。
#使用AD模块
Import-Module Activedirectory
#过期前10天提醒,过期不再提醒
$WarningDate=(Get-Date).AddDays(-170)
$GiveupDate=(Get-Date).AddDays(-180)
#过滤:账号有效,非长效密码,有邮箱
$FilterUser=Get-Aduser -searchbase "OU=分公司,OU=总公司,DC=domain,DC=com" -filter 'Enabled -eq "true" -and PasswordNeverExpires -eq "false" -and pwdLastSet -lt $WarningDate -and pwdLastSet -gt $GiveupDate -and mail -like "*"' | %{$_.Samaccountname}
foreach ($user in $FilterUser){
#邮件提醒
Send-MailMessage -from "no-reply@domain.com" -to "$user@domain.com" -subject "您的域账户密码即将过期" -body "您的域账户密码即将过期,请尽快修改密码。" -Attachments .\howtouse.pdf -smtpserver mail.domain.com) -Encoding ([System.Text.Encoding]::UTF8)
#记录用户
$user | Out-File -append -filepath .\user.txt
}