email

Wednesday, January 21, 2009

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

1 comment:

  1. See this link if you are looking for powershell code.

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

    ReplyDelete