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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
| Function LogFile ($output, $initLog)
{
if ($initLog -eq $True)
{
$input | out-file -filepath $output -encoding default -width 17384
}
else
{
$input | out-file -filepath $output -encoding default -width 17384 -append
}
}
function Send-Report
{
param($LogConent,$LogPath,$MailAddress)
try
{
Send-MailMessage -From "NO-Reply@contoso.com" -To $MailAddress -Subject 'Contoso Password check report' -Body $LogConent -Priority 'High' -SmtpServer mail.contoso.com -Port 25 -ErrorAction 'SilentlyContinue'
}
catch
{
$ErrorMessage = $Error[0].Exception.Message
Write-Host -ForegroundColor 'Red' "$(Get-Date -uFormat %Y%m%d-%H:%M:%S)" $ErrorMessage
("$(Get-Date -uFormat %Y%m%d-%H:%M:%S): " + $ErrorMessage) | LogFile -output $LogPath
}
}
#Main Code
#Import ActiveDirectory module
Import-Module ActiveDirectory
#Log initialization
[string]$LogDate = Get-Date -Format "yyyyMMdd"
$LogPath = "C:\PasswordLogs\DomainPasswordLog$LogDate.txt"
if ((Test-Path 'C:\PasswordLogs') -eq $false)
{
New-Item -ItemType directory 'C:\PasswordLogs' | Out-Null
}
#======================================================================================
#Get MaxPasswordAge
$RootDSE = Get-ADRootDSE
$PasswordPolicy = Get-ADObject $RootDSE.defaultNamingContext -Property maxPwdAge
$maxPwdAge = $PasswordPolicy.maxPwdAge/-864000000000
if (($maxPwdAge -eq 0) -or ($maxPwdAge -eq $null))
{
$ErrorMessage = "MaxPasswordAge is not correct"
Write-Host -ForegroundColor 'Red' "$(Get-Date -uFormat %Y%m%d-%H:%M:%S)" $ErrorMessage
("$(Get-Date -uFormat %Y%m%d-%H:%M:%S): " + $ErrorMessage) | LogFile -output $LogPath
$LogConent = Get-Content $LogPath -raw
Send-Report -LogConent $LogConent -LogPath $LogPath -MailAddress 'abc@contoso.com'
exit
}
#======================================================================================
#Check userlist
#我这里的用户列表是写在一个txt文档里的,这是因为在我的环境中大部分用户是不需要这种邮件提醒的,他们的账户会由我们负责维护
#如果需要在AD里检索需要检查的用户的话可以直接这样写$userList=Get-ADUser -Filter *|Select-Object -ExpandProperty SamAccountName
#这样的话下边这段就不需要了
$userList = "C:\Users\abc\UserList.txt"
if ((Test-Path $UserList) -eq $false)
{
$ErrorMessage = "Can't find userList.txt"
Write-Host -ForegroundColor 'Red' "$(Get-Date -uFormat %Y%m%d-%H:%M:%S)" $ErrorMessage
("$(Get-Date -uFormat %Y%m%d-%H:%M:%S): " + $ErrorMessage) | LogFile -output $LogPath
$LogConent = Get-Content $LogPath -raw
Send-Report -LogConent $LogConent -LogPath $LogPath -MailAddress 'abc@contoso.com'
exit
}
#======================================================================================
#这里如果是使用检索AD用户的方法的话可以直接写
#foreach($user in $userlist)替代get-content即可
Get-Content $UserList | %{
$name = $null
$userinfo = $null
$ExpireDate = $null
$PasswordSetDate = $null
$Today = $null
$leftDays = $null
$body = $null
$subject = $null
$IndividualPasswordPolicy = $null
$OutputMessage = $null
$name = $_
$userinfo = Get-ADUser -Identity $name -Properties *
#这里首先判断该用户信息是否存在,如果不存在直接进行记录即可
if ($userinfo -eq $null)
{
$ErrorMessage = $name + ": " + $Error[0].Exception.Message
Write-Host -ForegroundColor 'Red' "$(Get-Date -uFormat %Y%m%d-%H:%M:%S)" $ErrorMessage
("$(Get-Date -uFormat %Y%m%d-%H:%M:%S): " + $ErrorMessage) | LogFile -output $LogPath
}
else
{
if ($userinfo.PasswordNeverExpires -eq $true)
{
#这里记录谁的密码被设置为永久不过期了
$ErrorMessage = "$name's Password has been set to NeverExpires"
Write-Host -ForegroundColor 'Cyan' "$(Get-Date -uFormat %Y%m%d-%H:%M:%S)" $ErrorMessage
("$(Get-Date -uFormat %Y%m%d-%H:%M:%S): " + $ErrorMessage) | LogFile -output $LogPath
}
else
{
#这里会读取颗粒化密码策略的设置,它的优先级应该高于域策略的设置
$IndividualPasswordPolicy = (Get-AduserResultantPasswordPolicy $name)
if ($IndividualPasswordPolicy -ne $null)
{
$maxPwdAge = $IndividualPasswordPolicy.MaxPasswordAge.TotalDays
}
$PasswordSetDate = $userinfo.PasswordLastSet
$ExpireDate = $PasswordSetDate.AddDays($maxPwdAge)
$Today = Get-Date
#对比过期时间和今天,得出的数值就是还有多少天过期
$leftDays = (New-TimeSpan -Start $Today -End $ExpireDate).Days
if ($leftDays -lt 0)
{
$body = "
Dear $name ,
<p> Your Password has expired!!.<br>
Please change your Password as soon as possible so that you can work normally<br>
<p>Thanks, <br>
</P>"
$subject = "Your Password has expired!!"
$OutputMessage = "$(Get-Date -uFormat %Y%m%d-%H:%M:%S): $name's Password has expired"
Write-Output $OutputMessage | LogFile -output $LogPath
}
elseif ($leftDays -eq 1)
{
$body = "
Dear $name ,
<p> Your Password will expire in <b><font size=`"20px`" color=`"red`"> $leftDays </font></b> Day!!.<br>
Please change your Password as soon as possible so that you can work normally <br>
<p>Thanks, <br>
</P>"
$subject = "Your Password will expire in $leftDays day!!"
$OutputMessage = "$(Get-Date -uFormat %Y%m%d-%H:%M:%S): $name's Password will expire in $leftDays day"
Write-Output $OutputMessage | LogFile -output $LogPath
}
elseif ($leftDays -le 10)
{
$body = "
Dear $name ,
<p> Your Password will expire in <b><font size=`"20px`" color=`"red`"> $leftDays </font></b> Days!!.<br>
Please change your Password as soon as possible so that you can work normally <br>
<p>Thanks, <br>
</P>"
$subject = "Your Password will expire in $leftDays days"
$OutputMessage = "$(Get-Date -uFormat %Y%m%d-%H:%M:%S): $name's Password will expire in $leftDays days"
Write-Output $OutputMessage | LogFile -output $LogPath
}
else
{
$OutputMessage = "$(Get-Date -uFormat %Y%m%d-%H:%M:%S): $name's Password will expire in $leftDays days"
Write-Output $OutputMessage | LogFile -output $LogPath
}
#这里设置的是如果10天以内过期的话就会发送提醒
if ($leftDays -le 10)
{
#注意如果EmailAddress为空的话就需要自己处理如何找到邮件发送的地址了
$MailAddress = $userinfo.EmailAddress
if ($MailAddress -ne $null)
{
try
{
Send-MailMessage -From "No-Reply@contoso.com" -To $MailAddress -Subject $subject -Body $body -BodyAsHtml -Priority 'High' -SmtpServer mail.contoso.com -Port 25 -ErrorAction 'SilentlyContinue'
}
catch
{
$ErrorMessage = $Error[0].Exception.Message
Write-Host -ForegroundColor 'Red' "$(Get-Date -uFormat %Y%m%d-%H:%M:%S)" $ErrorMessage
("$(Get-Date -uFormat %Y%m%d-%H:%M:%S): " + $ErrorMessage) | LogFile -output $LogPath
}
}
}
}
}
}
#最后把这份报告发送给IT管理员
if ((Test-Path $LogPath) -eq $true)
{
$LogConent = Get-Content $LogPath -Raw
Send-Report -LogConent $LogConent -LogPath $LogPath -MailAddress 'it@contoso.com'
}
|