Hi guys
I have a script I've modified to go through the user accounts in the domain and get the password expire date based on our password expire policy of 90 days. Everything works great. I'm having an issue when sending an email to the users. I want to put a date when they need to change their passwords instead of a number of days. In the email subject the variable strNoOfDays is correct it'll say your password expires in 7 days or whatever. But the strChangeDate is not working. Help. Here's the script.
I have a script I've modified to go through the user accounts in the domain and get the password expire date based on our password expire policy of 90 days. Everything works great. I'm having an issue when sending an email to the users. I want to put a date when they need to change their passwords instead of a number of days. In the email subject the variable strNoOfDays is correct it'll say your password expires in 7 days or whatever. But the strChangeDate is not working. Help. Here's the script.
Code:
'==========================================================================
'
' NAME: Password Notification
'
' AUTHOR: Gene Magerr
' EMAIL: genemagerr@hotmail.com
'
' COMMENT: Original Script by [URL unfurl="true"]www.d2ww.com[/URL] Modified by Gene Magerr
'
' VERSION HISTORY:
' 1.0 10/03/2007 Initial release
'
'==========================================================================
' VBScript Prefix Naming Standard
'==========================================================================
' arr Array Contains an array of variables.
' bln Boolean Can contain either True or False
' byt Byte Integer value in the range of 0 to 255.
' col Collection Technically, a collection is not a variable subtype.
' It is mentioned in this table because it is good
' practice to use the col prefix to indicate a collection.
' Collections are used extensively in system administration
' scripts.
' Const Constant Constant values contain no prefix and use UPPER Case
' letters with underscores. Constants cannot be altered
' like normal variables.
' cur Currency Range of -922,337,203,685,477.5808 to
' 922,337,203,685,477.5807
' dbl Double Contains a double-precision floating-point number in
' the range
' dic Dictionary Scripting dictionaries
' dtm Date (Time) either a Date, Time, or Date and Time
' err Error Contains an error number value.
' fun Functions Programatic Function
' g_ Globals Variables with global scope
' int Integer Contains integer value in the range of -32,768 to 32,767.
' lng Long Contains an integer value in the range -2,147,483,648 To
' 2,147,483,647.
' obj Object Contains a reference to an Object.
' sng Single Contains a single-precision floating-point number
' str String A variable length string of textual data
' sub Subroutines Programatting SubRouting
' var Variant A variable that can store different data types at
' different times.
'==========================================================================
Option Explicit
'==========================================================================
' If TestMode is set to true, all wscript.echo messages will be displayed,
' if set to False no messages are displayed
'==========================================================================
TestMode = False
'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Dim objCommand
Dim objConnection
Dim strBase
Dim objFSO
Dim TestMode
Dim objFile
Dim strFilter
Dim strAttributes
Dim strPasswordChangeDate
Dim intPassAge
Dim lngTZBias
Dim objPwdLastSet
Dim strEmailAddress
Dim objShell
Dim lngBiasKey
Dim k
Dim PasswordExpiry
Dim strRootDomain
Dim strQuery
Dim objRecordset
Dim strName
Dim strCN
Dim strNoOfDays
Dim strChangeDate
Dim strPasswordExpiry
Dim strFirstName
Dim strLastName
'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
Const FOR_READING = 1, FOR_WRITING = 2, FOR_APPENDING = 8
'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
'If TestMode = True Then
'WScript.Echo "Put Message Here"
'End If
'==========================================================================
' Set the password expiration time and the domain
'==========================================================================
PasswordExpiry = 90
strRootDomain = "dc=mycompany,dc=org"
'==========================================================================
' Get the Active time on the Server
'==========================================================================
Set objShell = CreateObject("Wscript.Shell")
lngBiasKey = objShell.RegRead("HKLM\System\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias")
If UCase(TypeName(lngBiasKey)) = "LONG" Then
lngTZBias = lngBiasKey
ElseIf UCase(TypeName(lngBiasKey)) = "VARIANT()" Then
lngTZBias = 0
For k = 0 To UBound(lngBiasKey)
lngTZBias = lngTZBias + (lngBiasKey(k) * 256^k)
Next
End If
'==========================================================================
' Connect to Active Directory
'==========================================================================
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strBase = "<LDAP://" & strRootDomain & ">"
'==========================================================================
' Filter to exclude users that have "password never expires"
' or "password not required" set.
'==========================================================================
strFilter = "(&(objectCategory=person)(objectClass=user)" _
& "(!userAccountControl:1.2.840.113556.1.4.803:=65536)" _
& "(!userAccountControl:1.2.840.113556.1.4.803:=32))"
strAttributes = "SN,givenName,sAMAccountName,cn,mail,pwdLastSet"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
'==========================================================================
' Create the folder C:\logs if it doesn't exsist.
'==========================================================================
Set objFSO = CreateObject("Scripting.FilesystemObject")
If Not objFSO.FolderExists("C:\logs") Then
objFSO.CreateFolder("C:\logs")
End If
'==========================================================================
' Create the text file userpwd.txt in the C:\logs directory.
'==========================================================================
If Not objFSO.FileExists("C:\logs\userspwd.txt") Then
objFSO.CreateTextFile("C:\logs\userpwd.txt")
End If
Set objFile = objFSO.OpenTextFile("C:\logs\userpwd.txt", 8, True)
Do Until objRecordSet.EOF
strEmailAddress = objRecordSet.Fields("mail").value
strFirstName = objRecordSet.Fields("givenName").Value
strLastName = objRecordset.Fields("SN").Value
If strName = "" Then
strName = objRecordSet.Fields("samAccountName").Value
Else
strName = strFirstName & " " & strLastName
End If
strCN = objRecordSet.Fields("cn").Value
If strEmailAddress <> "" Then
'objFile.WriteLine "NT Name: " & strName & ", Common Name: " & strCN
Set objPwdLastSet = objRecordset.Fields("pwdLastSet").Value
strPasswordChangeDate = Integer8Date(objPwdLastSet, lngTZBias)
'objFile.WriteLine vbTab & "Password last changed at " & strPasswordChangeDate
intPassAge = DateDiff("d", strPasswordChangeDate, Now)
'objFile.WriteLine vbTab & "Password changed " & intPassAge & " days ago"
strChangeDate = DateAdd("d",PasswordExpiey,Date())
If intPassAge = (PasswordExpiry - 1) Then
objFile.WriteLine strName & vbCrLf & "Your password expires in 1 day"
objFile.WriteLine strEmailAddress & vbCrLf
Call SendEmailMessage(strEmailAddress, 1)
ElseIf intPassAge = (PasswordExpiry - 2) Then
objFile.WriteLine strName & vbCrLf & "Your password expires in 2 days"
objFile.WriteLine strEmailAddress & vbCrLf
Call SendEmailMessage(strEmailAddress, 2)
ElseIf intPassAge = (PasswordExpiry - 3) Then
objFile.WriteLine strName & vbCrLf & "Your password expires in 3 days"
objFile.WriteLine strEmailAddress & vbCrLf
'Call SendEmailMessage(strEmailAddress, 3)
ElseIf intPassAge = (PasswordExpiry - 4) Then
objFile.WriteLine strName & vbCrLf & "Your password expires in 4 days"
objFile.WriteLine strEmailAddress & vbCrLf
Call SendEmailMessage(strEmailAddress, 4)
ElseIf intPassAge = (PasswordExpiry - 5) Then
objFile.WriteLine strName & vbCrLf & "Your password expires in 5 days"
objFile.WriteLine strEmailAddress & vbCrLf
Call SendEmailMessage(strEmailAddress, 5)
ElseIf intPassAge = (PasswordExpiry - 6) Then
objFile.WriteLine strName & vbCrLf & "Your password expires in 6 days"
objFile.WriteLine strEmailAddress & vbCrLf
Call SendEmailMessage(strEmailAddress, 6)
ElseIf intPassAge = (PasswordExpiry - 7) Then
objFile.WriteLine strName & vbCrLf & "Your password expires in 7 days"
objFile.WriteLine strEmailAddress & vbCrLf
Call SendEmailMessage(strEmailAddress, 7)
ElseIf intPassAge = (PasswordExpiry - 14) Then
objFile.WriteLine strName & vbCrLf & "Your password expires in 14 days"
objFile.WriteLine strEmailAddress & vbCrLf
Call SendEmailMessage(strEmailAddress, 14)
ElseIf intPassAge > (PasswordExpiry) Then
objFile.WriteLine strName & vbCrLf & "Your password is " & intPassAge & " days old. "
objFile.WriteLine strEmailAddress & vbCrLf
End If
End If
objRecordSet.MoveNext
Loop
objConnection.Close
Function Integer8Date(objDate, lngBias)
Dim lngAdjust, lngDate, lngHigh, lngLow
lngAdjust = lngBias
lngHigh = objDate.HighPart
lngLow = objdate.LowPart
If lngLow < 0 Then
lngHigh = lngHigh + 1
End If
If (lngHigh = 0) And (lngLow = 0) Then
lngAdjust = 0
End If
lngDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _
+ lngLow) / 600000000 - lngAdjust) / 1440
On Error Resume Next
Integer8Date = CDate(lngDate)
If Err.Number <> 0 Then
On Error GoTo 0
Integer8Date = #1/1/1601#
End If
On Error GoTo 0
End Function
'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================
Sub SendEmailMessage(strDestEmail, strNoOfDays)
Dim objMessage
If (strDestEmail = "") Then
Wscript.Echo "No email address, no message sent."
Exit Sub
End If
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Password Expires in " & strNoOfDays & " days"
objMessage.Sender = "gmagerr@mycompany.org"
objMessage.To = "gmagerr@mycompany.org" 'strDestEmail
objMessage.TextBody = objMessage.TextBody & "On " & strChangeDate & ", the network password for " & strName & " will expire on unclassified systems." & vbCrLf
objMessage.TextBody = objMessage.TextBody & "Before that date (there is no grace period), please change your password." & vbCrLf
objMessage.TextBody = objMessage.TextBody & "IMPORTANT: Do not use a password you have used before." & vbCrLf
objMessage.TextBody = objMessage.TextBody & "If you have questions or need assistance, call the Computing Helpdesk, x6000." & vbCrLf
objMessage.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
objMessage.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = "mail.mycompany.org"
objMessage.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objMessage.Configuration.Fields.Update
objMessage.Send
'objMessage.Send
End Sub