AD 邮件账号到期提醒
2018-08-10 10:01
134 查看
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://dc01.domin.cn" $filter = "(&(objectcategory=domainDNS)(distinguishedName=DC=domin,DC=cn))" $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); $date_expire = $date_expire.ToString("(yyyy-M-d)") $SmtpClient = new-object system.net.mail.smtpClient $mailmessage = New-Object system.net.mail.mailmessage $SmtpClient.Host = "mail.domin.cn" $mailmessage.from = "邮件系统管理员<mailadmin@domin.cn>" $mailmessage.To.add($email) #$mailmessage.Bcc.add("spam_admin@domin.cn") $mailmessage.Subject = “【重要通知】您的邮件系统密码即将在 $remaining_day 天后到期,请尽快修改!” $mailmessage.IsBodyHtml = $true $encoding=[System.Text.Encoding]::unicode $mailmessage.Body = "尊敬的$name 您好,<br /><br />" $mailmessage.Body +="<strong> 您的邮件账号 <font color=red>$email</font> 密码将在 <font color=red><strong>$remaining_day</strong></font> 天后到期,失效日期:<font color=red><strong>$date_expire</strong></font> ,请您尽快修改密码,以免影响您正常使用邮件系统。</strong><br /><br />" $mailmessage.Body +="登录:https://mail.domin.cn 进行密码修改<br /><br />" $mailmessage.Body +="修改密码方法,请参照:<br /><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 = "mail.domin.cn" $mailmessage.from = "邮件系统管理员<mailadmin@domin.cn>" $mailmessage.To.add("liyong@domin.cn") #$mailmessage.Bcc.add("ithotline@domin.cn") $mailmessage.Subject = “[Report] domin.cn 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 = 7 ####这可修改7天前开始提醒 ############################################################################### #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) 5ac { $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_da 4fc3 te) $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 "<H3>Ad password expiration Status</H3> " -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 ###############################################################################
相关文章推荐
- AD账号密码过期邮件提醒-powershell实现--密码加密(新增)
- PowerShell检测AD域用户密码到期邮件提醒 推荐
- PowerShell检测AD域用户密码到期邮件提醒
- sharepoint2010人性化的地方--员工离职AD账号禁用(个人网站自动提醒上级经理功能)
- python实现 _ 图书馆书籍到期之前_自动邮件提醒
- AD离职人员账号清理并自动发送邮件
- sharepoint2010人性化的地方--员工离职AD账号禁用(个人网站自动提醒上级经理功能)
- 获取AD中即将过期账号并邮件通知
- Powershell管理系列(四十)PowerShell查询和解锁AD账号(改进后,只发一次邮件)
- 邮件提醒AD域用户更改密码
- 运维排错思路分享之“用户重复收到了密码到期提醒邮件?”
- sharepoint2010人性化的地方--员工离职AD账号禁用(个人网站自动提醒上级经理功能)
- Postfix 实例1―构建与AD集成的Linux邮件系统―项目说明
- 错误日志记录和邮件提醒的功能
- Linx监控分享--磁盘空间监控+邮件提醒
- 利用phpmailer自动发送邮件提醒
- RHEL4实现AD认证及邮件通知一体化系统(最终版)
- Gitlabr如何设置邮件提醒?
- 禁止你的iphone的记事本(note)和你的邮件账号同步