话说最近要求调整用户账户密码策略,需要用户定期修改密码,因此需要写一个脚本提醒员工到期修改密码。公司因刚刚与总公司分拆,现有域帐号与邮件服务器不是同一个域名,但是邮件服务器还是用总公司的域名,现有域帐户的邮件没有保存在mail字段而是新增加了一个字段单独存储,因此对一下脚本做了一些修改。实现的功能有:1.可以指定本ou及子ou内的所有用户 2.可支持中文邮件,并且带附件 3.使用外部邮件服务器发送,但是只支持匿名,因此有可能提示邮件仿冒。4.邮件为html格式
##################################################################################################################
# Please Configure the following variables....
$smtpServer="mail.game.com"
$expireindays = 45
###################################################################################################################
#Get Users From AD who are enabled
Import-Module ActiveDirectory
$users = get-aduser -filter * -properties * -searchbase 'OU=nimei,DC=nimei,DC=com' |where {$_.Enabled -eq "True"}
foreach ($user in $users)
{
if ($user.passwordexpired -eq "True")
{
write-host $user.displayname " Password Has Already Expired"
}
elseif ($user.passwordneverexpires -ne "True")
{
$passwordSetDate = $user.PasswordLastSet
$dfl = (get-addomain).DomainMode
if ($dfl -eq "Windows2008Domain")
{
$accountFGPP = Get-ADUserResultantPasswordPolicy $user
if ($accountFGPP -ne $null)
{
$maxPasswordAgeTimeSpan = $accountFGPP.MaxPasswordAge
}
else
{
$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
}
}
else
{
$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
}
if ($maxPasswordAgeTimeSpan -eq $null -or $maxPasswordAgeTimeSpan.TotalMilliseconds -eq 0)
{
Write-Host "MaxPasswordAge is not set for the domain or is set to zero!"
}
else
{
$today = get-date
$expireson = $passwordsetdate + $maxpasswordagetimespan
$daystoexpire = $expireson - $today
$date=$daystoexpire.days
if ($date -lt $expireindays)
{
$emailaddress = $null
$emailaddress = $user.mail
$username =$user.givenname
$AccountNameUser=$user.sAMAccountName
if ($emailaddress -ne $null)
{
$subject="您的域帐户密码即将在 $date 天后过期 "
$text="$username ,我们检测到您的域帐户 <$AccountNameUser>的密码即将在 $date 天后过期,请第一时间使用现有账户密码登录修改密码,如有其他问题, 请发邮件至it@example.com或拨打分机获得支持.
IT部"
$enc = New-Object System.Text.utf8encoding
Send-Mailmessage -smtpServer $smtpServer -from games.notice@renren-inc.com -to $emailaddress -subject $subject -body $text -priority High -Attachment "d:\域账户密码修改教程.pdf" -Encoding $enc -BodyAsHtml
}
}
}
}
}
转载于:https://blog.51cto.com/wang11/1216251