Powershell检测AD账户密码过期时间并邮件通知
脚本主要实现了两个功能 :
一能判断账户密码的过期时间并通过邮件通知到账户;
二是将这些即将过期的账户信息累计通知到管理员。
脚本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
############################################ #Author:Lixiaosong #For:检测AD密码过期时间并邮件通知 #Version:1.0 ############################################## Import-Module Activedirectory
$alladuser = get-aduser -searchbase "OU=IT,DC=contoso,DC=com" - filter * | %{ $_ .Samaccountname}
$userlist = @()
################################################# #检测AD密码过期时间并邮件通知相应账户 ################################################## foreach ( $user in $alladuser ){
#密码最后一次更改时间 $pwdlastset = Get-ADUser $user -Properties * | %{ $_ .passwordlastset}
#密码的过期时间 $pwdlastday =( $pwdlastset ).adddays(90)
#当前时间 $now = get-date
#判断账户是否设置了永不过期 $neverexpire = get-aduser $user -Properties * |%{ $_ .PasswordNeverExpires}
#距离密码过期的时间 $expire_days =( $pwdlastday - $now ).Days
#判断过期时间天小于15天的并且没有设置密码永不过期的账户 if ( $expire_days -lt 15 -and $neverexpire -like "false" ){
$chineseusername = Get-ADUser $user -Properties * | %{ $_ .Displayname}
#邮件正文
$Emailbody =
"亲爱的 $chineseusername 同学 : 您的域账户和邮箱密码即将在 $expire_days 天后过期, $pwdlastday 之后您将无法登陆计算机和收发邮件,请您尽快更改。
重置密码过程请遵循以下原则:
○密码长度最少 8 位;
○密码可使用最长时间 90天,过期需要更改密码;
○密码最短使用 1天( 1 天之内不能再次修改密码);
○强制密码历史 3个(不能使用之前最近使用的 3 个密码);
○密码符合复杂性需求(大写字母、小写字母、数字和符号四种中必须有三种、且密码口令中不得包括全部或部分用户名)
" Send-MailMessage -from "[email protected]" -to "[email protected]" -subject "您的账户密码即将过期" -body $Emailbody -Attachments D:\script\如何更改域用户密码.pptx -smtpserver mail.contoso.com -Encoding ( [System.Text.Encoding] ::UTF8)
############################################# #查找账户的密码过期时间并发送至管理员账户 ############################################# $username = Get-ADUser $user -Properties *
$userobject = New-object psobject
$userobject | Add-Member -membertype noteproperty -Name 用户名 -value $username .displayname
$userobject | Add-Member -membertype noteproperty -Name 邮箱 -Value $username .mail
$userobject | Add-Member -membertype noteproperty -Name 最后一次密码设置 -Value $username .Passwordlastset
$userobject | Add-Member -membertype noteproperty -Name 密码过期时间 -Value $pwdlastday
$userobject | Add-Member -membertype noteproperty -Name 距离密码过期天数 -Value $expire_days
$userlist += $userobject
} } $EmailbodyHTML = $userlist |
sort-object 距离密码过期天数 |
ConvertTo-Html |
Out-String Send-Mailmessage -from "[email protected]" –to “itmanager @contoso ” -Bodyashtml $EmailbodyHTML -Subject "管理员通知" -smtpserver mail.contoso.com -Encoding ( [System.Text.Encoding] ::UTF8)
|
实现的结果:
用户接收到的邮件:
管理员接收到的邮件:
本文出自 “运维人生” 博客,请务必保留此出处http://lixiaosong.blog.51cto.com/705126/1409113
本文转自 lorysun 51CTO博客,原文链接:http://blog.51cto.com/lorysun/1611061