AD账号解锁

时间:2021-07-25 09:08:13

Get-ADUser -Filter * -Properties *  -SearchBase "dc=uxin,dc=youxinpai,dc=com"| ? {$_.lockedout -eq "true"} |unlock-adaccount

账号解锁发邮件通知

Import-Module activedirectory

$users = Get-ADUser -Filter * -Properties *  -SearchBase "dc=uxin,dc=youxinpai,dc=com"| ? {$_.lockedout -eq "true"}
#$unames = $users|select samaccountname
$nls = ($users|measure).count
#$nls
If ($nls -gt 20)
{
foreach ($u in $users)
{
[array]$uus +=$u.samaccountname + "`n" Get-ADUser $u.samaccountname |Unlock-ADAccount
} #定义邮件服务器
$smtpServer = "mail.yy.com"
$smtpUser = "sysuser@yy.com"
$smtpPassword = "pass"
$mail = New-Object System.Net.Mail.MailMessage #定义发件人邮箱地址、收件人邮箱地址
$MailAddress="sysuser@yy.com"
$MailtoAddress="ITsupport@yy.com"
$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)
$mail.To.Add($MailtoAddress) #定义邮件标题、优先级和正文
$mail.Subject = "Locked User Number is $nls";
$mail.Priority = "High"
$mail.Body = "Locked AD Users have UNlocked.Users is:`n $uus"
$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword
$smtp.Send($mail) }