有一家企业,用户有两个邮件系统,用户在一个域中,而其中一个邮件系统是另一个活动目录森林的,往往密码过期而不知道,导致无法使用邮箱,下面的脚本可以通过邮件通知用户密码要到期了。
Get-ADUser -filter * -properties PasswordLastSet,EmailAddress,GivenName -SearchBase "OU=SH,DC=contoso,DC=com,DC=" |foreach {
$PasswordSetDate=$_.PasswordLastSet
$maxPasswordAgeTimeSpan = $null
$maxPasswordAgeTimeSpan = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
$today=get-date
$ExpiryDate=$passwordSetDate + $maxPasswordAgeTimeSpan
$daysleft=$ExpiryDate-$today
$display=$daysleft.days
$UserName=$_.GivenName
if ($display -lt 7){
$MyVariable = @"
" Dear $UserName
Your password will expire in $display days.If you are not going to change it you will not be able to connect to Irdeto corporate network.
If you are in Irdeto West HQ building follow the steps below to change your password:
- Press CTRL+ALT+DEL
- On the screen that came choose "Change password"
- Type in your old password and then type the new one (be advised you cannot use one of the previously used passwords)
- After the change is complete you will be prompted with information that passwor has been changed
With kind Regards
IT Department Frank
*** This is automatically generated email - please do not reply ***
"
"
send-mailmessage -to $_.EmailAddress -from
NoReply@contoso.com -Subject "IT Information: Your password will expire in $display days" -body $MyVariable -smtpserver mail.handpay.com.cn -Encoding([System.Text.Encoding]::UTF
}
}