Re: Need assistance badly!
- From: "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 20 Oct 2008 08:42:55 -0500
<JohnCee53@xxxxxxxxx> wrote in message
news:5d6b8cca-f006-41bf-a74c-3f1900c39c28@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I need your help. I'm not a scripter, I just edit existing scripts to
make them work for my environment. I've been tasked with making a
script that will run weekly to search Active Directory for users with
special prefixes (we have 4 of these groups) on their SAM Account
Names;
1) am.SAMAccountName
2) re.SAMAccountName
3) es.SAMAccountName
4) ic.SAMAccountName.
Once I have these results, they want them automatically emailed with a
format that shows the SAM Account Name and User Name, and last logon
time.
I have tried cobbling together a script that does this, but have not
gotten it correct to just show me both names at once, let alone
writing the output to a file and emailing it as the body of a
message.
Any help on this would be greatly appreciated!
I'm not sure what you mean. My guess is that the Common Name of the first
group of users is the string "am." followed by the value of the
sAMAccountName of the user. You don't say, but I also assume your domain is
at Windows Server 2003 functional level, so you can use the
lastLogonTimeStamp attribute. This means you can avoid the need to query
every DC in the domain to retrieve the largest lastLogon value for each
user.
I would use ADO in a VBScript program to retrieve all users with the
specifed prefixes. For details on using ADO see this link:
http://www.rlmueller.net/ADOSearchTips.htm
Using the syntax from the link, the filter could be:
strFilter = "(&(objectCategory=person)(objectClass=user)" _
& "(|(cn=am.*)(cn=re.*)(cn=es.*)(cn=ic.*)))"
The comma delimited list of attribute values to retrieve would be:
strAttributes = "sAMAccountName,cn,lastLogonTimeStamp"
This assumes that by "user name" you mean the Common Name of the user. To
convert the value of lastLogonTimeStamp (an Integer8 value) into a date/time
in the local time zone I would use the following function:
http://www.rlmueller.net/Programs/Integer8Date.txt
A script to dump out the values you need could be:
============
Option Explicit
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN
Dim objShell, lngBiasKey, lngTZBias
Dim objLast, dtmLast
' 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
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
' 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 with specified prefixes.
strFilter = "(&(objectCategory=person)(objectClass=user)" _
& "(|(cn=am.*)(cn=re.*)(cn=es.*)(cn=ic.*)))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,cn,lastLogonTimeStamp"
' 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
strCN = adoRecordset.Fields("cn").value
Set objLast = adoRecordset.Fields("lastLogonTimeStamp").Value
dtmLast = Integer8Date(objLast, lngTZBias)
Wscript.cho strName & "," & strCN & "," & CStr(dtmLast)
' 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
=========
This VBScript program can be run at a command prompt using the cscript host
so the output can be redirected to a text file. For example, if the program
is saved in the file GetUsers.vbs:
cscript //nologo GetUsers.vbs > report.csv
The file report.csv can be read into a spread*** (assuming that no user
names have embedded commas). The method you use to email depends on if you
use Outlook, CDO, or whatever. I personally use a third party tool I
purchased. You could compose and send one message per user in the Do Until
Loop that enumerates the recordset. Someone else may suggest how to do that.
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
.
- Follow-Ups:
- Re: Need assistance badly!
- From: JohnCee53
- Re: Need assistance badly!
- References:
- Need assistance badly!
- From: JohnCee53
- Need assistance badly!
- Prev by Date: Re: tools
- Next by Date: Re: tools
- Previous by thread: Need assistance badly!
- Next by thread: Re: Need assistance badly!
- Index(es):