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
--