Question : Send Email Notification on Password expiry in AD to users

I need to send email notification to users when their password is about to expire.
How can it be done ?

Answer : Send Email Notification on Password expiry in AD to users

The following is what I put together to use to alert a user and their manager of an upcoming account expiration based on the number of days.  I think that it is pretty cool.  Simply change the Where command to whatever LDAP you want to filter by.  
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:
On Error Resume Next 
Dim ExpirationDate, ManagerMail, UserMail, UserName, ExpDiff
LF = chr(10) + chr(13)
 
outfile="Expirations.csv"
Set fsomain=createobject("scripting.filesystemobject")
Set results=fsomain.createtextfile(outfile)
 
 
 
Const ADS_SCOPE_SUBTREE = 2
Const ADS_UF_ACCOUNTDISABLE = 2
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
 
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 5000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
"SELECT sAMAccountName, userAccountControl, accountExpires,givenName, mail, distinguishedname, cn, SN, department, manager, telephoneNumber FROM " _
& "'LDAP://ou=User accounts,dc=yourCompany,dc=org' WHERE " _
& "objectCategory='user' and accountExpires='*' and accountExpires<>'0' and accountExpires<>'9223372036854775807' and accountExpires<>'null'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
 
 
Do Until objRecordSet.EOF
 
    intUAC=objRecordSet.Fields("userAccountControl")
    If intUAC AND ADS_UF_ACCOUNTDISABLE Then
        objRecordSet.MoveNext
    End If
 
fullname = ""
ExpirationDate = ""
 
ManagerMail = ""
UserName = ""
fullname= objRecordSet.Fields("givenName").Value & " " & objRecordSet.Fields("SN").Value
UserName = objRecordSet.Fields("sAMAccountName").Value
UserMail = objRecordSet.Fields("mail").Value
Call GetExpirationDate(objRecordSet.Fields("distinguishedname").Value)
ExpDiff = DateDiff("d", CDate(Date), CDate(ExpirationDate))
If ExpDiff = 1 or ExpDiff =7 or ExpDiff = 14 or ExpDiff = 21 or ExpDiff = 28 Then
'If ExpDiff = 21 or ExpDiff = 28 Then
'If ExpDiff > -1 and expDiff < 15 then
Call GetManagerMail(objRecordSet.Fields("manager").Value)
Call MailAlert(ManagerMail)
Call MailAlert(UserMail)	
results.writeline UserName & ", " & ExpirationDate & ", " & ExpDiff & ", " & UserMail & ", " & ManagerMail
'wscript.echo UserName & ", " & ExpirationDate & ", " & ExpDiff & ", " & UserMail & ", " & ManagerMail
End If
 
objRecordSet.MoveNext
Loop
 
wscript.echo "Complete"
 
Function GetExpirationDate(DN)
 
'On Error Resume Next
strLDAP = "LDAP://" & DN
Set User = GetObject (strLDAP)
dtmAccountExpiration = User.AccountExpirationDate
If err.number = -2147467259 Or dtmAccountExpiration = "1/1/1970" Then
ExpirationDate = "No account expiration specified"
Else
ExpirationDate = User.AccountExpirationDate
ArrExpirationDate=Split(ExpirationDate)
ExpirationDate = RTrim(LTrim(ArrExpirationDate(0)))
End If
Set User = Nothing
'wscript.echo ExpirationDate
End Function
 
Function GetManagerMail(strObjectDN)
On Error Resume Next
	Set objUser = GetObject("LDAP://" & strObjectDN)
	ManagerMail = objUser.Get("mail")
End Function
 
 
 
'****************************************************************
  Sub MailAlert(Mail)
On Error Resume Next
	' Send by connecting to port 25 of the SMTP server.
	Const cdoSendUsingPort = 2
 
	set iMsg = CreateObject("CDO.Message")
	set iConf = CreateObject("CDO.Configuration")
	
	Set Flds = iConf.Fields
 
	' Set the CDOSYS configuration fields to use port 25 on the SMTP server
	With Flds
	    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
	    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.YourCompany.com" 
'	    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "waprdmsimc01" 
	    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10  
	    .Update
	End With
	
	' Build HTML for message body.
shtml =""
shtml = shtml & "

NT Expiration Notice

" shtml = shtml & "

Please review the expiration date for the NT ID below

" shtml = shtml & "

YourCompany Partner,

" shtml = shtml & "

You are receiving this email because our records indicate that the NT (Network)account for " & UserName & " is set to expire in " & ExpDiff & " days." shtml = shtml & " Contractors, please engage your manager to prevent service interruption.

" shtml = shtml & "

Managers,

" shtml = shtml & "

  • No action needed if: NT ID should expire on the expiration date" shtml = shtml & "

  • Verify the expiration date: check the Econometrix system by going to Search > Criteria....search for your contractor to find the end date." shtml = shtml & "
           o If date is accurate in Econometrix, extend the end date via the Expiration Automation Tool." shtml = shtml & "
           o If it is not accurate in Econometrix, submit an extension request via the Comsys Vendor Management System (VMS) - Econometrix." shtml = shtml & "
               ·If and when approved, extend the end date via the " shtml = shtml & "Expiration Automation Tool. If it's not approved no action is needed.

    " shtml = shtml & "

  • If you don't use the Econometrix VMS, extend the end date via the " shtml = shtml & "Expiration Automation Tool.

    " shtml = shtml & "
  • If this contractor/vendor no longer reports to you, forward this e-mail to the new manager.  New managers will" shtml = shtml & "need to “adopt” the contractor/vendor and extend the end date via the            " shtml = shtml & "
  • See How" shtml = shtml & "to Use the NT Exipration Automated Tool for instructions on using the tool.

    " shtml = shtml & "

    Note:" shtml = shtml & "VPN Certificates for non-YourCompany computers expire periodically. Renewing the NT" shtml = shtml & "account does not automatically renew the VPN Certificate. To renew the VPN" shtml = shtml & "Certificate, the Manager goes to Reliance and submits a request for VPN" shtml = shtml & "->Certificate Exception. Include an email approval from a Director.

    " shtml = shtml & "

    If you have any additional questions / comments, send mail to ESIS Specialists" shtml = shtml & "(NASA) in the GAL. 

    " shtml = shtml & "

    If you experience problems accessing this tool please contact the YourCompany EIT Helpdesk." shtml = shtml & " 

    " shtml = shtml & "

    Thank you for your continued support.

    " shtml = shtml & "

    " shtml = shtml & "

    Enterprise Information Security

    " shtml = shtml & "" ' Apply the settings to the message. With iMsg Set .Configuration = iConf .To = Mail ' .Bcc = "[email protected]" .From = "[email protected]" .Subject = "The account for " & fullname & " will expire in " & ExpDiff & " days on " & ExpirationDate & "." .HTMLBody = shtml ' .AddAttachment(sFolderPath & ZipFile) .Send End With ' Clean up variables. Set iMsg = Nothing Set iConf = Nothing Set Flds = Nothing End Sub '****************************************************************
  • Random Solutions  
     
    programming4us programming4us