Re: vbscript, searching Active directory using ADODB type mismatch error

From: Richard Mueller [MVP] (rlmueller-NOSPAM_at_ameritech.NOSPAM.net)
Date: 04/27/04


Date: Tue, 27 Apr 2004 13:13:09 -0500

Hi,

The LastLogon program goes to extra effort to retrieve the LastLogon dates
because this attribute is not replicated. The program must query every
domain controller in the domain. The latest lastLogon value is retained in a
dictionary object. The dictionary object can only keep track of pairs of
info, in this case the user Distinguished Name and the lastLogon date/time
value. In order to keep track of more information, you would instead have to
use a multi-dimensioned array. However, since the attributes you mention are
all replicated, it doesn't make sense to retrieve these values in a program
that queries every DC.

The best solution is to have two programs, one that retrieves the lastLogon
date for all users, then another to retrieve the other infomation. The
lastLogon values probably change often, while the second group of attributes
should seldom change. A program to retrieve NT name, description, and
homeDirectory for all users could be as follows (the description attribute
must be handled as a multi-valued attribute, even though it always has but
one value):

' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use ADO to search 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://" & strDNSDomain & ">"

' Filter on all user objects.
strFilter = "(&(objectCategory=person)(objectClass=user))"

' Specify attribute values to retrieve.
strAttributes = "distinguishedName,sAMAccountName,description,homeDirectory"

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

Do Until objRecordSet.EOF
  strDN = objRecordSet.Fields("distinguishedName")
  Wscript.Echo "User DN: " & strDN
  strNTName = objRecordSet.Fields("sAMAccountName")
  Wscript.Echo "-- NT name: " & strNTName
  arrDescription = objRecordSet.Fields("description")
  If IsNull(arrDescription) Then
    Wscript.Echo "-- Description: none"
  Else
    For Each strLine In arrDescription
      Wscript.Echo "-- Description: " & strLine
    Next
  End If
  strHomeDir = objRecordSet.Fields("homeDirectory")
  If IsNull(strHomeDir) Then
    Wscript.Echo "-- Home Directory: none"
  Else
    Wscript.Echo "-- Home Directory: " & strHomeDir
  End If
  objRecordSet.MoveNext
Loop

Alternatively, in the LastLogon program, you could bind to each user in the
final loop and retrieve the desired attributes. That would be considerably
slower, but that might not matter in a smaller network. The final loop could
be as follows:

For each strUser In objList
  Wscript.Echo strUser
  Wscript.Echo "-- Last Logon Date: " & objList(strUser)

  ' Bind to each user to retrieve more information.
  Set objUser = GetObject("LDAP://" & strUser)
  Wscript.Echo "-- Description: " & objUser.description
  Wscript.Echo "-- Home Directory: " & objUser.homeDirectory
Next

-- 
Richard
Microsoft MVP Scripting and ADSI
HilltopLab web site - http://www.rlmueller.net
--
"Paul" <paulwood11@hotmail.com> wrote in message
news:9D3C051F-B4E0-4795-8C4C-90C18578A438@microsoft.com...
> Thanks Richard, that part worked.
>
> My next question regrading this subject is that I am incorporating this
into your original last logon dates vb script from your web site.
>
> How do I output the name, description, & home directory after the loop
runs to determine the last login date in the script below?
>
> I tried working on it myself, by puting the output at the end of  original
loop without any sucess. It just outputs the incorrect last login date. I
have also tried adding the line below but I was unable to add the additional
fields to the objlist in the original loop so that line does not work
either.
>
> ' LastLogon.vbs
> ' VBScript program to determine when each user in the domain last logged
> ' on.
> '
> ' ----------------------------------------------------------------------
> ' Copyright (c) 2002 Richard L. Mueller
> .
> .
> .
> .
> .
>
> ' Output latest lastLogon date for each user.
> For Each strUser In objList
>   'Wscript.Echo strUser & " ; " & objList(strUser)
> Wscript.Echo  objList(strname) & " ; " & objList(strdescription) & " ; " &
objList(strhomedirectory) & " ; " & objList(strUser)
> Next
>
> Thank You,
>
> Paul
>
>
>
>
>
>


Relevant Pages

  • Re: List Last Logon Date
    ... Your script looks good. ... Unfortunately, adding lastLogon is complicated. ... If you have just one DC in your domain the replication issue doesn't matter. ... to retrieve. ...
    (microsoft.public.scripting.vbscript)
  • Re: this SHOULD be simple... any ideas?
    ... Save Domain Controller ... Instead of retrieving AdsPath, you would retrieve DNSHostName ... The ADO searches and the dictionary object must include all ... ' Output latest lastLogon date for each user. ...
    (microsoft.public.windows.server.active_directory)
  • Re: lastLogon
    ... This is a common problem. ... and your code will retrieve all user objects that have the ... The second code example you gave retrieves the value of lastLogon on only ... Please see my previous message for the script code. ...
    (microsoft.public.scripting.vbscript)
  • Re: lastLogon
    ... This is a common problem. ... and your code will retrieve all user objects that have the ... The second code example you gave retrieves the value of lastLogon on only ... Please see my previous message for the script code. ...
    (microsoft.public.scripting.vbscript)
  • Re: Script to obtain a report from AD
    ... > run monthly reports from AD and email the report to few members. ... Sending email depends on what is installed on the machine where the script ... I would use ADO to retrieve the information from AD. ... latest lastLogon value for the user. ...
    (microsoft.public.scripting.vbscript)