Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: Script for identifying Account to expire within X-Days then send notification email with list of users

Reply
Thread Tools Display Modes

Re: Script for identifying Account to expire within X-Days then send notification email with list of users

 
 
Pegasus [MVP]
Guest
Posts: n/a

 
      07-25-2009

"kevinguo" <> wrote in message
news:...
>
> 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?


 
Reply With Quote
 
 
 
 
Richard Mueller [MVP]
Guest
Posts: n/a

 
      07-25-2009

"Pegasus [MVP]" <> wrote in message
news:...
>
> "kevinguo" <> wrote in message
> news:...
>>
>> 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?


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\Co ntrol\" _
& "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 = ""
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
--


 
Reply With Quote
 
Pegasus [MVP]
Guest
Posts: n/a

 
      11-02-2009

"Guitar" <> wrote in message
news:...
>
> 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"?


 
Reply With Quote
 
Richard Mueller [MVP]
Guest
Posts: n/a

 
      11-02-2009

"Guitar" <> wrote in message
news:...
>
> 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
--


 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unable to send and receive email from msn account for 6 days ! ! Larz Windows Live Mail 14 06-01-2009 10:09 AM
Send notification before PWDs expire best@news.postalias Active Directory 11 02-23-2007 01:09 PM
Re: AD Script to set passwords to expire in 10 days Joe Kaplan Active Directory 6 01-05-2007 11:06 PM
Get a List of All the Users Whose Passwords Never Expire Misha Scripting 2 04-12-2006 11:58 AM
Password Expire email notification Mahar Active Directory 8 06-20-2005 11:49 AM



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