Home > Scripting > Script to generate last logon time of all users

Script to generate last logon time of all users

SBelow code give the last logon time of all users in active directory. Please drop me a mail, if you want any further fine tuning to this script or incase of queries.

I am providing this script without any warranty. Use at your own choice. :-)

Code:

############# Getlastlogon.vbs ################

on error resume next

Set objConnection = CreateObject(“ADODB.Connection”)
objConnection.Open “Provider=ADsDSOObject;”

Set objCommand = CreateObject(“ADODB.Command”)
objCommand.ActiveConnection = objConnection

objCommand.CommandText = _
“<LDAP://dc=mydomain,dc=com>;” & _
“(&(objectCategory=person)(objectClass=user));” & _
“ADsPath;subtree”

Set objRecordSet = objCommand.Execute

While Not objRecordset.EOF
strADsPath = objRecordset.Fields(“ADsPath”)
Set objUser = GetObject(strADsPath)
set objLogon = objUser.Get(“lastLogonTimestamp”)
intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
intLogonTime = intLogonTime / (60 * 10000000)
intLogonTime = intLogonTime / 1440
WScript.Echo “Approx last logon timestamp for ” & strADsPath & intLogonTime + #1/1/1601#
objRecordset.MoveNext
Wend
objConnection.Close
##################################

Note: you can redirect the script output from command prompt to clipboard by using http://www.sitaram-pamarthi.com/2009/01/copy-command-output-to-clip-board.html

Syntax: cscript Getlastlogon.vbs clip

Happy Learning…
Sitaram Pamarthi

Categories: Scripting
  1. Sitaram Pamarthi
    December 3, 2009 at 1:48 pm | #1

    See this link if you are looking for powershell code.

    http://www.sitaram-pamarthi.com/2009/12/powershell-commands-to-list-domain.html

  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>