NOVOTS KMS 词汇表 Glossary    联系我们 Contact Us
查询 Search  
   
按类别浏览 Browse by Category
NOVOTS KMS .: 沟通技巧 .: AD域用户密码到期邮件提醒脚本

AD域用户密码到期邮件提醒脚本

AD域用户密码到期邮件提醒脚本该脚本可以实现密码到期邮件提醒,给管理员发送报告,而且是HTML格式的邮件,界面更加友好。

cls
#############################################################################
# Description: The current script send Alert for users before they password
# expires. You can set some values to configure this script.
############################################################################

###############################################################################
# Get The max Password age from AD
###############################################################################

function Get-maxPwdAge{
$root = [ADSI]"LDAP://mydomain.local"
$filter = "(&(objectcategory=domainDNS)(distinguishedName=DC=codespring,DC=local))"
$ds = New-Object system.DirectoryServices.DirectorySearcher($root,$filter)
$dc = $ds.findone()

[int64]$mpa = ($dc.Properties[‘maxpwdage’][0]).ToString().Trim("-")

return $mpa*(.000000100)/86400
}


###############################################################################
# Function to send email to each user
###############################################################################
function send_email_user ($remaining_day, $email, $name )
{
        $today = Get-Date
        $date_expire = [DateTime]::Now.AddDays($remaining_day) ;
    $SmtpClient = new-object system.net.mail.smtpClient
    $mailmessage = New-Object system.net.mail.mailmessage
    $SmtpClient.Host = "smtp.mydomain.local"
    $mailmessage.from = "it@mydomain.local"
    $mailmessage.To.add($email)        
        $mailmessage.Bcc.add("it-reports@mydomain.local")
    $mailmessage.Subject = “$name, your password expires on mydomain.local ”
    $mailmessage.IsBodyHtml = $true
    $mailmessage.Body = "<h1>Dear $name </h1>"
        $mailmessage.Body +="<h5> Your password for account <font color=red>$email</font> will be expirend in <font color=red><strong>$remaining_day</strong></font> days on <strong>$date_expire</strong></h5>"
        $mailmessage.Body +="For other question please ask the Administrators !<br /><br />"
    $mailmessage.Body += " Generated on : $today<br /><br />"
        $mailmessage.Body += "================================== <br />"
        $mailmessage.Body += "mydomain.local <br />"
    $smtpclient.Send($mailmessage)
}

###############################################################################
# Send REPORT for Admins
###############################################################################
function sendmail($body)
{
        $today = Get-Date
    $SmtpClient = new-object system.net.mail.smtpClient
    $mailmessage = New-Object system.net.mail.mailmessage
    $SmtpClient.Host = "smtp.mydomain.local"
    $mailmessage.from = "it@mydomain.local"
    $mailmessage.To.add("it-reports@mydomain.local")
    $mailmessage.Subject = “[Report] mydomain.local password expires”
    $mailmessage.IsBodyHtml = $true
    $mailmessage.Body = "<h4>Generated on : $today `n</h4>"  + $body
    $mailmessage.Body += "`n" +  $body1
       
    $smtpclient.Send($mailmessage)
}


###############################################################################
# Search for the active directory users with following conditions
# 1. Is in USER category
# 2. Is loged in more that 1 times - for eliminate the system accounts
# 3. Eliminate the Disbaled Accounts
###############################################################################
$strFilter = "(&(objectCategory=User)(logonCount>=1)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$colResults = $objSearcher.FindAll();

#SET the max day  before expiration alert
$max_alert = 10

###############################################################################
#SET the max password lifetime
# In the future i rewrite to ask teh GP for the group.
###############################################################################

$max_pwd_life= get-maxPwdAge;


$userlist = @()

foreach ($objResult in $colResults)

        {$objItem = $objResult.Properties;
        if ( $objItem.mail.gettype.IsInstance -eq $True)
                {               
                        $user_name = $objItem.name
                        $user_email = $objItem.email
                        #Transform the DateTime readable
                        $user_logon = [datetime]::FromFileTime($objItem.lastlogon[0])
                        $result =  $objItem.pwdlastset
                        $user_pwd_last_set = [datetime]::FromFileTime($result[0])                       
                       
                        #calculate the difference in Day
                        $diff_date = [INT]([DateTime]::Now - $user_pwd_last_set).TotalDays;
                                               
                       
                        if (($max_pwd_life - $diff_date) -le $max_alert) {
                                $selected_user = New-Object psobject
                                $selected_user | Add-Member NoteProperty -Name "Name" -Value  $objItem.name[0]
                                $selected_user | Add-Member NoteProperty -Name "Email" -Value  $objItem.mail[0]
                                $selected_user | Add-Member NoteProperty -Name "LastLogon" -Value $user_logon
                                $selected_user | Add-Member NoteProperty -Name "LastPwdSet" -Value $user_pwd_last_set
                                $selected_user | Add-Member NoteProperty -Name "EllapsedDay" -Value $diff_date
                                $selected_user | Add-Member NoteProperty -Name "RemainingDay" -Value ($max_pwd_life-$diff_date)
                                $userlist+=$selected_user
                       
                        }
                }
        }
       
###############################################################################
# Send email for each user
###############################################################################
        foreach ($userItem in $userlist )
        {
                send_email_user $userItem.RemainingDay $userItem.Email $userItem.Name
        }
       
###############################################################################
# Sedn email for Admins in reporting format
###############################################################################
        $bodyme = $userlist| Sort-Object "RemainingDay" |  ConvertTo-Html -Title "AD password Status" -Body "<H2>Ad password expiration Status</H2> "  -head "<style>td{font-size:smaller;padding:0 0 0 5px;border: 1px solid #003366;}table{border: 1px solid #003366;margin:0;padding:0}tr{margin:0;padding:0;}h2{color:red};th{font-size:smaller;text-align:left;border: 1px solid #003366;background-color:#aaa;}</style>" | foreach {$_ -replace "<table>", "<table cellspacing=0>"}
       
        sendmail $bodyme
       
               
###############################################################################
# END
###############################################################################

修改下Get-maxPwdAge后面的域名,修改成自己的域。


这篇文章对你多有用?

相关文章

article Outlook 2013新增功能之”忘记粘贴附件提醒“
 在Outlook 2013

  3-25-2014    Views: 1581   
article SQL Server下利用vbscript 实现数据库月备份自动清理并发邮件提醒
SQL Server下利用vbscript...

  10-10-2009    Views: 2139   
article 使用VBS发邮件提醒用户更改密码的邮件提醒
'**********************************************...

(No rating)  1-14-2014    Views: 1094   

用户评语

添加评语
Comment jiangjl <jiangjl@lenovots.com>
7-16-2013 at 4:11pm

没有介绍脚本的使用方法
没有介绍脚本的使用方法


.: .: .: .: .:
[ 登陆 ]
北京护航科技有限公司 2006

Novots Technologies Limited