根据指定的用户清单去修改域用户的密码,并相应发送新密码邮件给到用户
$xiaoxie =@('a','b','c','d','e','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z')
$daxie =@('A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z')
$shuzi =@('2','3','4','5','6','7','8','9')
$fuhao =@('&','$','-','=')
$xxxx =@('xiaoxie','daxie','shuzi','fuhao')
$hashtable = New-Object System.Collections.Hashtable
$hashtable= @{
'xiaoxie' = $xiaoxie
'daxie' = $daxie
'shuzi' = $shuzi
'fuhao' = $fuhao
}
$mimacount =10
function suijimima {
$countsuiji=$xxxx|Get-Random
$a =[string]($hashtable.$countsuiji|Get-Random )
$a
}
Import-Module ActiveDirectory
$SMTPServer = "XXXXXX"
$SMTPPort = "587"
$SMTPAccount = "XXXXXXXXXX"
$SecureFile = "D:pwd.txt"
$SecurePwd = Get-Content $SecureFile | ConvertTo-SecureString
$SMTPCred = New-Object System.Management.Automation.PSCredential($SMTPAccount,$SecurePwd)
$EmailLast = "XXXXXXXXXX"
$AllUser = $(cat C:\Users\Administrator.ADSH01\Desktop\需要修改密码域账号账号.txt)
$Count = 0
foreach ($User in $AllUser){
$PwdLastset = Get-ADUser $User -Properties passwordlastset | % {$_.passwordlastset}
$PwdLastday = ($PwdLastset).AddDays(90)
$Now = Get-Date
$ExpireDays = ($PwdLastday - $Now).Days
if($ExpireDays -gt "20"){
$Count = $Count + 1
$bx=""
for ($i=1;$i -lt $mimacount+1;$i++)
{
$bx2=suijimima
$bx += $bx2
}
$User | Set-ADAccountPassword -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $bx -Force)
$EmailAddress = "$User$EmailLast"
$EmailSubject = "过期账号密码已修改"
$Emailbody =
@"
您的域账号 $User 密码已经修改为 $bx 。
"@
Send-MailMessage -UseSsl -Credential $SMTPCred -From $SMTPAccount -Subject $EmailSubject -To $EmailAddress -Body $Emailbody -Encoding ([System.Text.Encoding]::UTF8) -Port $SMTPPort -SmtpServer $SMTPServer
Send-MailMessage -UseSsl -Credential $SMTPCred -From $SMTPAccount -Subject $EmailSubject -To "821271702@qq.com" -Body $Emailbody -Encoding ([System.Text.Encoding]::UTF8) -Port $SMTPPort -SmtpServer $SMTPServer
}
}
$EmailSubject1 = "修改密码数量 "
$Emailbody1 =
@"
XXXXXX域控 一共发出 $Count 份邮件
"@
Send-MailMessage -UseSsl -Credential $SMTPCred -From $SMTPAccount -Subject $EmailSubject1 -To "821271702@qq.com" -Body $Emailbody1 -Encoding ([System.Text.Encoding]::UTF8) -Port $SMTPPort -SmtpServer $SMTPServer