WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Determine LastLogin

 Keywords:  Determine Get LastLogin user object AD DC last Login Check DC's  time


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;      Code to determine LastLogin for a user object in AD (windows 2003)
;      -First gathers all of the DC's to determine the count
;      -Next loops through each DC to get the lastlogin information for the specified user
;      -For each loop, the lastlogin is checked to the current from the DC queried and determines
;            if it is the most recent time.  If it is more recent then the last, it updates the tDiff2
;            variable for checking against other times.
;      -As it goes through each loop, it converts the time to days for reporting
;      -There are a couple of error traps in case the user path or lastlogin are empty, it does
;         a GOTO to the bottom of the loop.
;      -Finally, it presents the most recent lastLogin by time (YmdHms) and days.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

AddExtender("WWADS44I.DLL")

samName = "simpsonh"

dc = "NULL"
lastLogon = "*Never Logged On*" ; if no lastlogin found
lastTime = ""
tDiff2 = "0000:00:00:00:00:00" ;preset for first diff
dFlg = 0 ; initial diff flag
ouPath = ""
sAdsiPath="LDAP://rootDSE"
sValue=dsGetProperty (sAdsiPath,"defaultNamingContext")

; Get DC server object paths.
; You will have to put your DC path here
dsDCPath =  "LDAP://yourdomain/OU=Domain Controllers,DC=generic,DC=domain,DC=net"
dcObjs = dsFindPath(dsDCPath, "(&(objectCategory=computer)(sAMAccountName=*))")
dcCnt = ItemCount(dcObjs, @TAB)

; Check all DC's for lastLogin time
info = ""
For jj = 1 To dcCnt
   Error = 0 ; reset error variable for each loop

   ; extract the dc path from the list of domain controllers
   ; and strip out just the name of the computer
   dcName = ItemExtract(1, ItemExtract(2, ItemExtract(jj, dcObjs, @TAB), "="), ",")

   ; set filter for pre-check of lastlogon property for given sAMAccountName
   sFilter = "(&(objectCategory=Person)(lastlogon=*)(sAMAccountName=%samName%))"
   dcPath = StrCat("LDAP://%dcName%/",sValue)
   ouPath = dsFindPath(dcPath, sFilter)

   ; If user object has a lastlogon attribute
   ; get the info
   If ouPath <> ""
      LastError()
      ErrorMode(@OFF)
      objUser = ObjectOpen(ouPath)
      If Error == 1129 Then Goto bypass

      If objUser Then last = objUser.lastlogin
      ErrorMode(@CANCEL)
      If last == 0 Then Goto bypass

      ; perform time diff check against each lastLogin
      ; found to determine most recent
      tDiff = TimeDiff(TimeYmdHms(), last)
      If tDiff >= tDiff2 && dFlg == 0
         dDiff = TimeDiffDays(TimeYmdHms(), last)
         lastLogon = dDiff
         lastTime = last
         dc = dcName
         dFlg = 1
         tDiff2 = tDiff
      Else
         If tDiff < tDiff2
            dDiff = TimeDiffDays(TimeYmdHms(), last)
            lastLogon = dDiff
            lastTime = last
            dc = dcName
            tDiff2 = tDiff
         EndIf
      EndIf
   :bypass
   EndIf
Next
objUser = ""

Exit

Article ID:   W17543
Filename:   Determine LastLogin.txt
File Created: 2010:10:01:14:21:02
Last Updated: 2010:10:01:14:21:02