Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Usernames last used

Hi,

How can i get all the last used dates from all the users in the domain.

I have many common users created and dout if they are being used so need to take a report and delete them if not used.

Regards
Sharath
Avatar of Jeremy Weisinger
Jeremy Weisinger

Oops, that was for each computer.
Google has tons of links (I'm sure at least one of them are pointing here
http://www.google.com/search?hl=en&safe=active&client=firefox-a&rls=org.mozilla%3Aen-US%3Aofficial&hs=JAh&q=script+to+retrieve+last+logon+date&btnG=Search

But to assist in narrowing down a selection
http://www.rlmueller.net/Last%20Logon.htm
Avatar of bsharath

ASKER

I get this error.

C:\>cscript Lastlogon.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\Lastlogon.vbs(154, 1) Microsoft VBScript runtime error: Variable is undefined
: 'server1'
This is onemore error.

C:\>cscript //nologo LastLogon.vbs
CN=Guest,OU=Former Colleagues,OU=User Accounts,OU=IND,OU=Countries,DC=Developmen
t,DC=Group,DC=co,DC=uk;Never
C:\LastLogon.vbs(78, 5) Microsoft VBScript runtime error: Object doesn't support
 this property or method

I have tried many which are availabe on the net but gives me errors.
What script are you running?

"Variable undefined" more or less means  that your using a variable that wasn't listed at the beginning of the script.(all those "Dim" statements at the beginning)

 Did you alter the script at all? If so can you post it?
This is the script.

' LastLogonTimeStamp.vbs
' VBScript program to determine when each user in the domain last logged
' on. Domain must be at Windows Server 2003 Functional Level.
'
' ----------------------------------------------------------------------
' Copyright (c) 2007 Richard L. Mueller
' Hilltop Lab web site - http://www.rlmueller.net
' Version 1.0 - March 24, 2007
'
' The lastLogonTimeStamp attribute is Integer8, a 64-bit number
' representing the date as the number of 100 nanosecond intervals since
' 12:00 am January 1, 1601. This value is converted to a date. The last
' logon date is in UTC (Coordinated Univeral Time). It must be adjusted
' by the Time Zone bias in the machine registry to convert to local
' time.
'
' You have a royalty-free right to use, modify, reproduce, and
' distribute this script file in any way you find useful, provided that
' you agree that the copyright owner above has no warranty, obligations,
' or liability for such use.

Option Explicit

Dim objRootDSE, adoConnection, adoCommand, strQuery
Dim adoRecordset, strDNSDomain, objShell, lngBiasKey
Dim lngBias, k, strDN, dtmDate, objDate, lngDate
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)
    Next
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,lastLogon"

' 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
    lngDate = adoRecordset.Fields("lastLogon").Value
    ' Convert Integer8 value to date/time in current time zone.
    On Error Resume Next
    Set objDate = lngDate
    If (Err.Number <> 0) Then
        On Error GoTo 0
        dtmDate = #1/1/1601#
    Else
        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#
        Else
            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"
    Else
        Wscript.Echo strDN & ";" & dtmDate
    End If
    adoRecordset.MoveNext
Loop

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

Is there any thing i need to change
Click Start -> Run -> Cmd.exe

DSQuery * domainroot -filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=*))" -attr distinguishedName  lastLogon -limit 0

OR (save result in file)

DSQuery * domainroot -filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=*))" -attr distinguishedName  lastLogon -limit 0 >C:\Result.txt

OR (with time)

DSQuery * domainroot -filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=*))" -attr distinguishedName  lastLogon lastLogonTimestamp -limit 0 >C:\Result.txt

OR (Username)

DSQuery * domainroot -filter "(&(objectCategory=Person)(objectClass=User)(sAMAccountName=*))" -attr displayName lastLogon -limit 0 >C:\Result.txt

Check Result.txt file on C: drive root for output.
farhankazi
I get the lastlogon time as this
   128231107305474400
Is there any way to get it to right format as dd/mm/yy
DumpSec, can also provide user last logged on information. On the menu bar choose Reports, Dump users as a table, choose LastLogonTime and add, then OK to generate report;
http://www.somarsoft.com/
ASKER CERTIFIED SOLUTION
Avatar of Farhan Kazi
Farhan Kazi
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
farhankazi:

I get this.

---------------------------
Windows Script Host
---------------------------
Script:      C:\Lastlogon.vbs
Line:      1
Char:      1
Error:      Invalid character
Code:      800A0408
Source:       Microsoft VBScript compilation error

---------------------------
OK  
---------------------------
Did read the instructions? :)

:: * Copy and paste following script in notepad and save it with any name having .cmd extension.

NOT with extension VBS.
Sorry my mistake :)
No worries :) Thanks