In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Recently, we want to realize the function of automatically sending email to remind domain users when their passwords expire. It feels easier to use PowerShell.
Most of the information found is AD and Exchange installed together, not practical.
The following code is an integration of online information, the actual code can be run.
Main reference: www.nibayuan.com/2014/12/31/task-ad-password-expires-email/
http://blog.csdn.net/andrewniu/article/details/52594318
Description:
The server is windows server 2012 r2 std Chinese version
This server is DC only and has no other functions.
Import-Module Activedirectory
$alladuser=get-aduser -searchbase "ou=xxx,dc=xxxxx,dc=xxxx" -filter * | %{$_.Samaccountname}
#Fill in "ou=***,dc=***,dc=***" above according to the actual situation of your domain structure
$userlist = @()
echo $alladuser#Show all users
#echo $userlist
$itmag = "xxxxx@xxxxxx.com" #Email address of IT administrator
function sendmail($mailaddr,$body) #Defines how to send mail
{
$msg=New-Object System.Net.Mail.MailMessage
$msg.To.Add($mailaddr)
#$msg.Bcc.Add($itmag)#cc to admin
$msg.From = New-Object System.Net.Mail.MailAddress("xxxxx@xxxxxx.com", "xxxx",[system.Text.Encoding]::GetEncoding("UTF-8")) #From
$msg.Subject = "Message password expiration alert"
$msg.SubjectEncoding = [system.Text.Encoding]::GetEncoding("UTF-8")
$msg.Body =$body
#$Attachments=New-Object System. Net.Mail.Attachment("D:\Documents\xxxx.zip")#Create Attachments
#$msg.Attachments.add($Attachments) #Add attachments, English name can be more than one, Chinese name can only be one.
$msg.BodyEncoding = [system.Text.Encoding]::GetEncoding("UTF-8")
$msg.IsBodyHtml = $false#Send html formatted email
#$msg.Priority = [System.Net.Mail.MailPriority]::High
$client = New-Object System. Net.Mail.SmtpClient("smtp.xxxxxxx.cn") #Configure smtp server
$client.Port = 587#Specify smtp port
$client.EnableSsl = $true #smtp server with ssl
$client.UseDefaultCredentials = $false
$client.Credentials=New-Object System.Net.NetworkCredential("xxxx@xxx.com", "*********")
try {$client.Send($msg)}
catch [Exception]
{$($_.Exception.Message)
$mailaddr
}
}
foreach ($user in $alladuser)
{
#Last password change time
$pwdlastset=Get-ADUser $user -Properties * | %{$_.passwordlastset}
#Password expiration time
$pwdlastday=$pwdlastset.AddDays(90)
#Current time
$now=get-date
#Determine whether the account is set to never expire
$neverexpire=get-aduser $user -Properties * |%{$_.PasswordNeverExpires}
#Time since password expires
$expire_days=($pwdlastday - $now).Days
#Determine the account whose expiration time is less than 5 days and greater than-5 days (i.e. expired 5 days) and no password is set.
if($expire_days -lt 5 -and $expire_days -gt -5 -and $neverexpire -like "false" )
{
$chineseusername= Get-ADUser $user -Properties * | %{$_.Displayname}
#Message body
$Emailbody=
"Dear $chineseusername :
Your email password will expire in $expire_days, please change it as soon as possible.
Please follow these guidelines when changing your password:
○ Password length at least 8 digits;
○ The password can be used for up to 90 days, and the password needs to be changed when it expires;
○ The password must be used for a minimum of 1 day (the password cannot be changed again within 1 day);
○ Mandatory password history 3 (the last 3 passwords cannot be used);
○ Passwords meet complexity requirements (three out of four must be capital letters, lowercase letters, numbers, and symbols, and passwords must not include all or part of the username)
"
$tomailaddr = $user + "@xxxx.com"
#echo $tomailaddr
sendmail $tomailaddr $Emailbody
}
}
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.