Automatic Password Expiration Email

This script will go through the OU you specify and email the users that their password will expire.

  1. Dim oConnection 'As ADODB.Connection
  2. Dim oRecordSet 'As ADODB.RecordSet
  3. Dim strQuery 'As String
  4. Dim strDomainNC 'As String
  5. Dim oRootDSE 'As IADs
  6. Dim oDirObject 'As Variant
  7. Dim vArgs, x
  8. Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000
  9. Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D
  10.  
  11. On Error Resume Next
  12.  
  13. ' Find the domain naming context
  14. set oRootDSE = GetObject("LDAP://RootDSE")
  15. strDomainNC = oRootDSE.Get("defaultNamingContext")
  16. set oRootDSE = Nothing
  17.  
  18. ' Setup the ADO connection
  19. Set oConnection = CreateObject("ADODB.Connection")
  20. oConnection.Provider = "ADsDSOObject"
  21. oConnection.Open "ADs Provider"
  22. Set vArgs = WScript.Arguments
  23.  
  24. if VArgs.Count <> 2 Then
  25.    wscript.echo "USAGE: cscript expirepassemail.vbs  "
  26.    wscript.echo "USAGE: cscript expirepassemail.vbs 110 120"
  27.    wscript.quit
  28. end if
  29.  
  30. min = vArgs(0)
  31. max = vArgs(1)
  32.  
  33. Set oCommand = CreateObject("ADODB.Command")
  34. Set oCommand.ActiveConnection = oConnection
  35. oCommand.CommandText= "," &amp; strDomainNC &amp; ">;(objectCategory=user);distinguishedName,cn,name;subTree"
  36. oCommand.Properties("searchscope") = 2
  37. oCommand.Properties("Page Size") = 1000
  38. oCommand.Properties("Timeout") = 15
  39. Set oRecordSet = oCommand.Execute
  40.  
  41. if oRecordSet.Eof then
  42.   response.write "No objects were found"
  43.   WScript.Quit(0)
  44. Else
  45.   Dim vClasses 'As Variant
  46.   Dim strClass 'As String
  47.   Dim mysid 'As variant
  48.  
  49.   wscript.echo "List of users and password information"
  50.   wscript.echo "           Max: " &amp; max &amp; ", Min: " &amp; min
  51.   wscript.echo "--------------------------------------"
  52.  
  53. '  Iterate through the objects that are in the query results
  54.  
  55.    While Not oRecordset.Eof
  56.       Set usr = GetObject("LDAP://" &amp; oRecordset.Fields("distinguishedName").Value)
  57.       If (instr(usr.SamAccountName, "$") = 0) and instr(usr.adspath, ".Global") = 0 Then
  58.          Err.Number = 0
  59.          dtmValue = Usr.PasswordLastChanged
  60.          If Err.Number <> E_ADS_PROPERTY_NOT_FOUND Then
  61.             flags = usr.get("userAccountControl")
  62.             If flags And ADS_UF_DONT_EXPIRE_PASSWD Then
  63.                expire = "noexpire"
  64.             Else
  65.                expire = "Expires"
  66.             End if
  67.             If (int(DateDiff("d", dtmValue, Date)) > int(min)) and (int(DateDiff("d", dtmValue, Date)) < int(max)) Then
  68.                wscript.echo usr.cn &amp; " " &amp; expire &amp; " in " &amp; 120-DateDiff("d", dtmValue, Date) &amp; " days"
  69.                if 120-DateDiff("d", dtmValue, Date) < 0 then
  70.                   wscript.echo "      Expired! " &amp; 120-DateDiff("d", dtmValue, Date)
  71.                   Set objMessage = CreateObject("CDO.Message")
  72.                   objMessage.Subject = usr.cn &amp; " Expired"
  73.                   objMessage.Sender = "DoNotReply "
  74.                   objMessage.From = "Do Not Reply"
  75.                   objMessage.To = ""
  76.                   objMessage.TextBody = usr.samaccountname &amp; " has already expired."
  77.                   objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  78.                   objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = ""
  79.                   objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  80.                   objMessage.Configuration.Fields.Update
  81.                   ' objMessage.Send
  82.                else
  83.                   response.write 120-DateDiff("d", dtmValue, Date)
  84.                   Set objMessage = CreateObject("CDO.Message")
  85.                   objMessage.Subject = "Your network and email password is expiring"
  86.                   objMessage.Sender = "DoNotReply "
  87.                   objMessage.From = "Do Not Reply"
  88.                   objMessage.To = usr.samaccountname &amp; "@"
  89.                   objMessage.TextBody = usr.samaccountname &amp; "@ : Your password will expire in " &amp; 120-DateDiff("d", dtmValue, Date) &amp; " days!  Please change it to avoid disruptions."
  90.                   objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  91.                   objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = ""
  92.                   objMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  93.                   objMessage.Configuration.Fields.Update
  94.                   objMessage.Send
  95.                end if
  96.             End If
  97.          End If
  98.       End If
  99.     oRecordset.MoveNext
  100.     Wend
  101. End If
  102.  
  103. 'Clean up
  104. Set oRecordset = Nothing
  105. Set oConnection = Nothing

 
 
 

Leave a Reply