I want to query AD to fetch last login for all user in AD. Do you have VB script that I can utilize?

Posted on 2007-10-03
Last Modified: 2008-09-13
Win2003. I want to obtain a list of all users in AD along with last login. As an example query should fetch Last name, First name, description, Login ID and Last Login field. I an OU that has 12000 users in it and a few with less 500 users. Is there a script that I could run to accomplish this?
Question by:zohra01
    LVL 70

    Accepted Solution

    LVL 12

    Assisted Solution


    Can you try the code below? We can modify this to give all the information if this is what you want

    Option Explicit

    Dim objRootDSE, adoConnection, adoCommand, strQuery
    Dim adoRecordset, strDNSDomain, objShell, lngBiasKey
    Dim lngBias, k, strDN, dtmDate, objDate
    Dim strBase, strFilter, strAttributes, lngHigh, lngLow

    ' 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)
    End If
    Set objShell = Nothing

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

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

    ' Search entire domain.
    strBase = "<LDAP://" & strDNSDomain & ">"

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

    ' Comma delimited list of attribute values to retrieve.
    strAttributes = "distinguishedName,lastLogonTimeStamp"

    ' Construct the LDAP syntax query.
    strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

    ' Run the query.
    adoCommand.CommandText = strQuery
    adoCommand.Properties("Page Size") = 100
    adoCommand.Properties("Timeout") = 60
    adoCommand.Properties("Cache Results") = False
    Set adoRecordset = adoCommand.Execute

    ' Enumerate resulting recordset.
    Do Until adoRecordset.EOF
       ' Retrieve attribute values for the user.
        strDN = adoRecordset.Fields("distinguishedName").Value
        ' Convert Integer8 value to date/time in current time zone.
        On Error Resume Next
        Set objDate = adoRecordset.Fields("lastLogonTimeStamp").Value
        If (Err.Number <> 0) Then
            On Error GoTo 0
            dtmDate = #1/1/1601#
            On Error GoTo 0
            lngHigh = objDate.HighPart
            lngLow = objDate.LowPart
            If (lngLow < 0) Then
                lngHigh = lngHigh + 1
            End If
            If (lngHigh = 0) And (lngLow = 0 ) Then
                dtmDate = #1/1/1601#
                dtmDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _
                    + lngLow)/600000000 - lngBias)/1440
            End If
        End If
        ' Display values for the user.
        If (dtmDate = #1/1/1601#) Then
            Wscript.Echo strDN & ";Never"
            Wscript.Echo strDN & ";" & dtmDate
        End If

    ' Clean up.
    Set adoConnection = Nothing
    Set adoCommand = Nothing
    Set adoRecordset = Nothing
    Set objDate = Nothing

    LVL 65

    Assisted Solution

    Hi zohra01, this post can be easily modified to get the details you need into a CSV file:

    We can help you further if needed......I'm pretty sure that particular one could actually be shortened, seeing as you want "all" users, you won't require the filtering....



    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
    On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
    This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now