Guest User

Email Expiration VB Script

a guest
Aug 8th, 2013
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.85 KB | None | 0 0
  1. '====================================
  2. 'Script Configuration Options
  3. Const EMAIL_SERVER = "{MAILSERVER DNS OR IP ADDRESS}"
  4. Const EMAIL_FROM = "{[email protected]"
  5. Const OWA_STRING = "at {MAIL_SERVER_OWA)"
  6.  
  7.  
  8. Const FIRST_REMINDER_DAY = 5
  9. Const START_REMINDER_DAYS = 2
  10.  
  11. 'Where log files will be stored
  12. 'Remember to end with \
  13. Const LOG_PATH = "C:\scripts_Logs\"
  14. 'This setting allows you to append the date to the log file so that you get an Archive
  15. Const APPEND_DATE = 0
  16.  
  17. 'SET DEBUG MODE to 1 to send all emails to debug_email
  18. DEBUG_MODE = 0
  19. DEBUG_EMAIL = "{PERSONAL_TEST}@DOMAIN.COM"
  20. '====================================
  21.  
  22.  
  23. Const ADS_UF_PASSWD_CANT_CHANGE = &H40
  24. Const ADS_UF_DONT_EXPIRE_PASSWD = &H10000
  25. Const ADS_UF_ACCOUNTDISABLE = &H02
  26.  
  27.  
  28. Dim strFilePath, objFSO, objFile, adoConnection, adoCommand
  29. Dim objRootDSE, strDNSDomain, strFilter, strQuery, adoRecordset
  30. Dim strDN, objShell, lngBiasKey, lngBias, blnPwdExpire,blnAccountDisabled
  31. Dim objDate, dtmPwdLastSet, lngFlag, k, oDomain, maxPwdAge, numDays,whenPasswordExpires, strEmailMessage
  32. Dim strDomain
  33.  
  34. '====================================
  35. 'Script to change a filename using timestamps
  36. Dim strMonth, strDay
  37. strMonth = DatePart("m", Now())
  38. strDay = DatePart("d",Now())
  39.  
  40. if Len(strMonth)=1 Then
  41. strMonth = "0" & strMonth
  42. Else
  43. strMonth = strMonth
  44. end If
  45.  
  46. if Len(strDay)=1 Then
  47. strDay = "0" & strDay
  48. else
  49. strDay = strDay
  50. end If
  51. '===================================
  52.  
  53. If APPEND_DATE=true Then
  54. strFilePath = LOG_PATH & "users_DOMAIN_" & DatePart("yyyy",Now()) & strMonth & strDay & ".txt"
  55. Else
  56. strFilePath = LOG_PATH & "users_DOMAIN.txt"
  57. End If
  58.  
  59. Set objFSO = CreateObject("Scripting.FileSystemObject")
  60.  
  61. ' Open the file for write access.
  62. On Error Resume Next
  63. Set objFile = objFSO.OpenTextFile(strFilePath, 2, True, 0)
  64. If (Err.Number <> 0) Then
  65. On Error GoTo 0
  66. Wscript.Echo "File " & strFilePath & " cannot be opened"
  67. Set objFSO = Nothing
  68. Wscript.Quit(1)
  69. End If
  70. On Error GoTo 0
  71.  
  72. ' Obtain local time zone bias from machine registry.
  73. Set objShell = CreateObject("Wscript.Shell")
  74. lngBiasKey = objShell.RegRead("HKLM\System\CurrentControlSet\Control\" _
  75. & "TimeZoneInformation\ActiveTimeBias")
  76. If (UCase(TypeName(lngBiasKey)) = "LONG") Then
  77. lngBias = lngBiasKey
  78. ElseIf (UCase(TypeName(lngBiasKey)) = "VARIANT()") Then
  79. lngBias = 0
  80. For k = 0 To UBound(lngBiasKey)
  81. lngBias = lngBias + (lngBiasKey(k) * 256^k)
  82. Next
  83. End If
  84.  
  85. ' Use ADO to search the domain for all users.
  86. Set adoConnection = CreateObject("ADODB.Connection")
  87. Set adoCommand = CreateObject("ADODB.Command")
  88. adoConnection.Provider = "ADsDSOOBject"
  89. adoConnection.Open "Active Directory Provider"
  90. Set adoCommand.ActiveConnection = adoConnection
  91.  
  92. ' Determine the DNS domain from the RootDSE object.
  93. Set objRootDSE = GetObject("LDAP://RootDSE")
  94. strDNSDomain = objRootDSE.Get("DefaultNamingContext")
  95.  
  96.  
  97. strDomain = Replace(strDNSDomain, ",DC=",".")
  98. strDomain = Replace(strDomain, "DC=","")
  99.  
  100.  
  101. strBase = "<LDAP://" & strDNSDomain & ">"
  102.  
  103. ' Filter to retrieve all user objects.
  104. strFilter = "(&(objectCategory=person)(objectClass=user))"
  105.  
  106. ' Filter to retrieve all computer objects.
  107. ' strFilter = "(objectCategory=computer)"
  108.  
  109. strAttributes = "displayName,pwdLastSet,userAccountControl,mail"
  110.  
  111. strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
  112.  
  113. adoCommand.CommandText = strQuery
  114. adoCommand.Properties("Page Size") = 100
  115. adoCommand.Properties("Timeout") = 30
  116. adoCommand.Properties("Cache Results") = False
  117.  
  118. ' Iterate thru the users collection in Active Directory
  119. objFile.WriteLine "DISPLAY NAME , PASSWORD EXPIRES, ACCOUNT DISABLED, PASSWORD LAST SET , EMAIL, PASSWORD EXPIRES, NUMBER OF DAYS"
  120.  
  121. Set oDomain = GetObject("LDAP://" & strDNSDomain)
  122. Set maxPwdAge = oDomain.Get("maxPwdAge")
  123.  
  124.  
  125. numDays = ((maxPwdAge.HighPart * 2 ^ 32) + maxPwdAge.LowPart) / -864000000000
  126.  
  127.  
  128. Set adoRecordset = adoCommand.Execute
  129. Do Until adoRecordset.EOF
  130.  
  131. Set objDate = adoRecordset.Fields("pwdLastSet").Value
  132.  
  133. lngFlag = adoRecordset.Fields("userAccountControl").Value
  134. blnPwdExpire = True
  135. dtmPwdLastSet = Integer8Date(objDate, lngBias)
  136. whenPasswordExpires = DateAdd("d", numDays, dtmPwdLastSet)
  137.  
  138. If ((lngFlag And ADS_UF_PASSWD_CANT_CHANGE) <> 0) Then
  139. blnPwdExpire = False
  140. End If
  141.  
  142. If ((lngFlag And ADS_UF_DONT_EXPIRE_PASSWD) <> 0) Then
  143. blnPwdExpire = False
  144. End If
  145.  
  146. If (lngFlag And ADS_UF_ACCOUNTDISABLE) <> 0 Then
  147. blnAccountDisabled=True
  148. Else
  149. blnAccountDisabled=False
  150.  
  151. If IsNull(adoRecordset.Fields("mail").Value) or IsEmpty(adoRecordset.Fields("mail").Value) Then
  152.  
  153. Else
  154. 'check if password expires
  155. If blnPwdExpire = True Then
  156. If (DateDiff("d", Now, whenPasswordExpires) <= START_REMINDER_DAYS AND DateDiff("d", Now, whenPasswordExpires) >=0) Or (DateDiff("d", Now, whenPasswordExpires) = FIRST_REMINDER_DAY) Then
  157. strEmailMessage="1" 'password will expire in less than 14 days
  158. objFile.WriteLine adoRecordset.Fields("displayName").Value & "," & blnPwdExpire & " , " & blnAccountDisabled & " , " & dtmPwdLastSet & " , " & adoRecordset.Fields("mail").Value & "," & whenPasswordExpires & "," & DateDiff("d", Now, whenPasswordExpires)
  159. Call sendEmail(adoRecordset.Fields("mail").Value,FormatDateTime(whenPasswordExpires,2),strEmailMessage, adoRecordset.Fields("displayName").Value, strDomain)
  160. ElseIf DateDiff("d", Now, whenPasswordExpires) <0 Then
  161. strEmailMessage="0" 'password has already expired
  162. objFile.WriteLine adoRecordset.Fields("displayName").Value & "," & blnPwdExpire & " , " & blnAccountDisabled & " , " & dtmPwdLastSet & " , " & adoRecordset.Fields("mail").Value & "," & whenPasswordExpires & "," & DateDiff("d", Now, whenPasswordExpires)
  163. Call sendEmail(adoRecordset.Fields("mail").Value,FormatDateTime(whenPasswordExpires,2),strEmailMessage, adoRecordset.Fields("displayName").Value, strDomain)
  164. End If
  165. End If
  166.  
  167. End If
  168. End If
  169.  
  170. adoRecordset.MoveNext
  171. Loop
  172.  
  173. adoRecordset.Close
  174.  
  175. ' Clean up.
  176. objFile.Close
  177. adoConnection.Close
  178. Set objFile = Nothing
  179. Set objFSO = Nothing
  180. Set objShell = Nothing
  181. Set adoConnection = Nothing
  182. Set adoCommand = Nothing
  183. Set objRootDSE = Nothing
  184. Set adoRecordset = Nothing
  185.  
  186. Wscript.Echo "Done"
  187.  
  188. '=============================
  189. 'Function -Integer8 attribute function courtesy of Richard Mueller - http://www.rlmueller.net/Integer8Attributes.htm
  190. Function Integer8Date(ByVal objDate, ByVal lngBias)
  191. ' Function to convert Integer8 (64-bit) value to a date, adjusted for
  192. ' local time zone bias.
  193. Dim lngAdjust, lngDate, lngHigh, lngLow
  194. lngAdjust = lngBias
  195. lngHigh = objDate.HighPart
  196. lngLow = objdate.LowPart
  197. ' Account for bug in IADslargeInteger property methods.
  198. If (lngLow < 0) Then
  199. lngHigh = lngHigh + 1
  200. End If
  201. If (lngHigh = 0) And (lngLow = 0) Then
  202. lngAdjust = 0
  203. End If
  204. lngDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _
  205. + lngLow) / 600000000 - lngAdjust) / 1440
  206. Integer8Date = CDate(lngDate)
  207. End Function
  208.  
  209. '=============================
  210. 'Send Email function
  211. Sub sendEmail(strEmail, expirationDate1, msgFlag, Name, domain)
  212. 'Accept input parameters
  213. Dim email
  214. Dim expirationDate
  215. Dim strMessage
  216. Dim strName
  217. Dim strDomain
  218. Dim strMesagept2
  219. email= strEmail
  220. expirationDate= expirationDate1
  221. strMessage= msgFlag
  222. strName = Name
  223. strDomain = domain
  224.  
  225. If strMessage=1 then
  226. strMessage="will expire on"
  227. strMessage2= "Password Expiration"
  228. ElseIf strMessage=0 Then
  229. strMessage="has expired last"
  230. strMessage2= "Expired Password"
  231. End If
  232.  
  233. Set objMessage = CreateObject("CDO.Message")
  234. objMessage.Subject = "Message Alert: " & strMessage2
  235. objMessage.From = EMAIL_FROM
  236.  
  237. If DEBUG_MODE=1 Then
  238. objMessage.To = DEBUG_EMAIL
  239. else
  240. objMessage.To = email
  241. End If
  242.  
  243. objMessage.TextBody = "Hello " & strName & ","& VbCrLf & VbCrLf _
  244. & "Your " & strDomain & " Domain password " & strMessage & " " & FormatDateTime(expirationDate,1) & "."_
  245. & " Please Update your password as soon as possible."& VbCrLf & VbCrLf _
  246. & "If you are on a PC that is on the Domain You can change it by pressing ALT-CTRL-DEL and then Clicking On Change Password" & VbCrLf & VbCrLf _
  247. & "If you are on a Mac or the above does not work for you, you can change it by:"& VbCrLf & VbCrLf _
  248. & "1." & vbTab & "Going to OWA " & OWA_STRING & VbCrLf _
  249. & "2." & vbTab & "Clicking on Options on the Top left" & VbCrLf _
  250. & "3." & vbTab & "Clicking on Change Password"& VbCrLf _
  251. & vbTab & "a. The Domian is " & strDomain & VbCrLf & VbCrLf & VbCrLf
  252.  
  253. '==This section provides the configuration information for the remote SMTP server.
  254. '==Normally you will only change the server name or IP.
  255. objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  256.  
  257. 'Name or IP of Remote SMTP Server
  258. objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = EMAIL_SERVER
  259.  
  260. 'Server port number(typically 25)
  261. objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  262.  
  263. objMessage.Configuration.Fields.Update
  264.  
  265. objMessage.Send
  266. Set objMessage = Nothing
  267. End Sub
Advertisement
Add Comment
Please, Sign In to add comment