Discussion:
Script for identifying Account to expire within X-Days then send notification email with list of users
(too old to reply)
kevinguo
2009-07-25 06:42:07 UTC
Permalink
I recevied a task to create a script to collect Account to expire withi
15Days and mail notification.I search for Microsoft online help .don'
got the answer.
I could really use some help.
Thanks

--
kevingu
-----------------------------------------------------------------------
kevinguo's Profile: http://forums.techarena.in/members/118011.ht
View this thread: http://forums.techarena.in/server-scripting/1220209.ht

http://forums.techarena.i
Pegasus [MVP]
2009-07-25 08:34:49 UTC
Permalink
I recevied a task to create a script to collect Account to expire within
15Days and mail notification.I search for Microsoft online help .don't
got the answer.
I could really use some help.
Thanks,
--
kevinguo
You need to think a little more about this task, in particular about the
following points:
- Is this a once-only exercise or some ongoing effort?
- Is this a domain environment? Workgroup?
- Do you want to run the script on a server? Or on each workstation? When
the user logs on?
- Who should be the recipient of the mail notification?
- Why not collect the names of all expiring accounts in some log file?
Richard Mueller [MVP]
2009-07-25 13:05:52 UTC
Permalink
Post by Pegasus [MVP]
I recevied a task to create a script to collect Account to expire within
15Days and mail notification.I search for Microsoft online help .don't
got the answer.
I could really use some help.
Thanks,
--
kevinguo
You need to think a little more about this task, in particular about the
- Is this a once-only exercise or some ongoing effort?
- Is this a domain environment? Workgroup?
- Do you want to run the script on a server? Or on each workstation? When
the user logs on?
- Who should be the recipient of the mail notification?
- Why not collect the names of all expiring accounts in some log file?
If you email, the method used will depend on your environment. The example
below, from a previous newsgroup posting, assumes that CDO is available on
your computer. It also assumes you have a domain. The email sender address
is hard coded. The email messages are sent to the users whose accounts are
about to expire, and it is assumed that the "mail" attribute of the user
object has the correct email address. This is the field on the "General" tab
of ADUC for each user. If you have Exchange, you may need to use the
proxyAddresses attribute, which is multi-valued, and determine which value
is the "primary" address (the one where the prefix, like SMTP: or X400:, is
all capital letters).
=============
' VBScript program to find all user accounts that expire between
' now and a specified number of days in the future.
' Email notification to "mail" attribute of user.

Option Explicit

Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName
Dim objDate, dtmAcctExpires, dtmDate1, dtmDate2, intDays
Dim lngSeconds1, str64Bit1, lngSeconds2, str64Bit2
Dim objShell, lngBiasKey, lngBias, k, strMail

' Specify days in the future. We retrieve all user accounts that
' expire between now and this many days in the future.
intDays = 14

' Our first date/time value is now.
dtmDate1 = Now()

' Our second date/time value is the specified number of days
' in the future.
dtmDate2 = DateAdd("d", intDays, dtmDate1)

' Obtain local Time Zone bias from machine registry.
Set objShell = CreateObject("Wscript.Shell")
lngBiasKey = objShell.RegRead("HKLM\System\CurrentControlSet\Control\" _
& "TimeZoneInformation\ActiveTimeBias")
If (UCase(TypeName(lngBiasKey)) = "LONG") Then
lngBias = lngBiasKey
ElseIf (UCase(TypeName(lngBiasKey)) = "VARIANT()") Then
lngBias = 0
For k = 0 To UBound(lngBiasKey)
lngBias = lngBias + (lngBiasKey(k) * 256^k)
Next
End If

' Convert both datetime values to UTC.
dtmDate1 = DateAdd("n", lngBias, dtmDate1)
dtmDate2 = DateAdd("n", lngBias, dtmDate2)

' Find number of seconds since 1/1/1601 for each date.
lngSeconds1 = DateDiff("s", #1/1/1601#, dtmDate1)
lngSeconds2 = DateDiff("s", #1/1/1601#, dtmDate2)

' Convert the number of seconds to strings
' and convert to 100-nanosecond intervals.
str64Bit1 = CStr(lngSeconds1) & "0000000"
str64Bit2 = CStr(lngSeconds2) & "0000000"

' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on user objects where accountExpires is
' between now and the specified number of days in the future.
strFilter = "(&(objectCategory=person)(objectClass=user)" _
& "(accountExpires>=" & str64Bit1 & ")(accountExpires<=" _
& str64Bit2 & "))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,accountExpires,mail"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values and display.
strName = adoRecordset.Fields("sAMAccountName").Value
strMail = adoRecordset.Fields("mail").Value
Set objDate = adoRecordset.Fields("accountExpires").Value
dtmAcctExpires = Integer8Date(objDate, lngBias)
Call SendEmailMessage(strName, CStr(dtmAcctExpires), strMail)
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close

Function Integer8Date(ByVal objDate, ByVal lngBias)
' Function to convert Integer8 (64-bit) value to a date, adjusted for
' local time zone bias.
Dim lngAdjust, lngDate, lngHigh, lngLow
lngAdjust = lngBias
lngHigh = objDate.HighPart
lngLow = objdate.LowPart
' Account for error in IADsLargeInteger property methods.
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
' Trap error if lngDate is ridiculously huge.
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

Sub SendEmailMessage(strUser, strExpires, strEmail)
' Subroutine to send an email message using CDO.

Dim objMessage

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Account " & strUser & " Expires " & strExpires
objMessage.Sender = "***@mycompany.com"
objMessage.To = strEmail
objMessage.TextBody = "User account " & strUser _
& " expires " & strExpires & "."
objMessage.Send

End Sub
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
kevinguo
2009-07-26 07:18:28 UTC
Permalink
Thx Richard.
My work environment is domain&#65292;I review your script which work
fine.Thank you for quickly response

--
kevingu
-----------------------------------------------------------------------
kevinguo's Profile: http://forums.techarena.in/members/118011.ht
View this thread: http://forums.techarena.in/server-scripting/1220209.ht

http://forums.techarena.i
Guitar
2009-11-02 06:03:33 UTC
Permalink
I have a Window 2003 Standard Domain, I don't need to send email but I
would like it to output the soon-to-be expiring users account into a
text file.

I cut-paste the above script and saved it as UserExpiring.vbs and I ran
it but nothing came back.

Any advise as what I'm doing wrong? I ran it via DOS prompt cscript
UserExpiring.vbs > accounts.txt
--
Guitar
------------------------------------------------------------------------
Guitar's Profile: http://forums.techarena.in/members/150865.htm
View this thread: http://forums.techarena.in/server-scripting/1220209.htm

http://forums.techarena.in
Pegasus [MVP]
2009-11-02 06:50:03 UTC
Permalink
Post by Guitar
I have a Window 2003 Standard Domain, I don't need to send email but I
would like it to output the soon-to-be expiring users account into a
text file.
I cut-paste the above script and saved it as UserExpiring.vbs and I ran
it but nothing came back.
Any advise as what I'm doing wrong? I ran it via DOS prompt cscript
UserExpiring.vbs > accounts.txt
--
Guitar
Which "above script"?
Richard Mueller [MVP]
2009-11-02 14:58:12 UTC
Permalink
Post by Guitar
I have a Window 2003 Standard Domain, I don't need to send email but I
would like it to output the soon-to-be expiring users account into a
text file.
I cut-paste the above script and saved it as UserExpiring.vbs and I ran
it but nothing came back.
Any advise as what I'm doing wrong? I ran it via DOS prompt cscript
UserExpiring.vbs > accounts.txt
--
Guitar
------------------------------------------------------------------------
Guitar's Profile: http://forums.techarena.in/members/150865.htm
View this thread: http://forums.techarena.in/server-scripting/1220209.htm
http://forums.techarena.in
I'll just take a guess that the code you refer to is similar to code I
posted in the newsgroups in mid September. If so, replace these two lines:

Call SendEmailMessage(strEmail, strName, dtmExpires)
Wscript.Echo "Message for " & strName & " sent to " & strEmail

with something like this:

Wscript.Echo strName & "," & dtmExpires

Then run the script at a command prompt using the cscript host, so you can
redirect the output to a text file. For example, if the script is saved in a
file called UserExpiring.vbs, the command could be:

cscript //nologo UserExpiring.vbs > report.txt

The //nologo optional parameter suppresses logo information you don't want
in the text file. This assumes you are in the folder where the file
UserExpiring.vbs is saved. Otherwise, you must include the full path to the
file. The new file report.txt will be created in the current directory. If
you use the Wscript.Echo statement I suggest above, you will get a comma
delimited file with the name and expiration date (you might want to name the
file with *.csv extension so you can easily open it in a spreadsheet
program).

Even if the program you refer to is not the one I posted, the suggestion
above should help. You need to replace the statements that email the message
with ones that echo the required information to the console, so you can
redirect the output to a text file.
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
Loading...