Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


lastLogonTimestamp

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,23841
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> lastLogonTimestamp
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 lastLogonTimestamp - 7/20/2005 7:08:37 AM   
  bkhsms

 

Posts: 7
Score: 0
Joined: 7/8/2005
Status: offline
VB newbie here so be gentle.  I'm trying to set a resultant set query given a base query to Active Directory for the lastLogonTimestamp attribute.  I grabbed the basics from the MS site and wrote more to get the base DN for the query and the number of weeks to query for.  My intent is to query through the base DN and return the user name and lastlogontimestamp date in an Excel spreadsheet.  First things first though as I'm just now echoing the output:

'This portion of the code presents which level to start they query
Input1 = InputBox("Enter the base DN to start the query:" _
               & " I.E. dc=ridley,dc=com","Last Logon Timestamp")
strDN = Input1
If strDN = "" Then
              MsgBox "You must enter a valid start DN", 64, "Alert"
              Wscript.Quit
End if
'This portion of the code requests the week range to check
Input2 = InputBox("Number of weeks since last logon","Last Logon Timestamp")
strWeeks = Input2
If strWeeks = "" Then
              MsgBox "You must enter a valid week range", 64, "Alert"
              WScript.Quit
End if
'Get the user info in the base DN
Set objOU = GetObject("LDAP://" & strDN)
For Each objObject In objOU
              set strUser = objObject.Name
' Begin calculation
set objLogon = strUser.Get("lastLogonTimeStamp")
intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
intLogonTime = intLogonTime / (60 * 10000000)
intLogonTime = intLogonTime / 1440
intLLTS = intLogonTime + #1/1/1601#
strDays = strWeeks * 7
intReqCompare = Now - strDays
If intLLTS < intReqCompare Then
               wscript.echo objUser.Name
               wscript.echo objUser.lastLogonTimeStamp
End If
Next
 
 
Post #: 1
 
 RE: lastLogonTimestamp - 7/20/2005 7:33:22 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
What is the problem?

(in reply to bkhsms)
 
 
Post #: 2
 
 RE: lastLogonTimestamp - 7/20/2005 7:37:35 AM   
  bkhsms

 

Posts: 7
Score: 0
Joined: 7/8/2005
Status: offline
Now the problem.  Line 19 where it begins with setting the string User:  strUser = obj.objObject.Name  An error is displayed when running the script:

Object required: '[string:  "CN=[user]"]'
800A01A8

I'm not calling this from .asp .

(in reply to bkhsms)
 
 
Post #: 3
 
 RE: lastLogonTimestamp - 7/20/2005 7:46:21 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Well, assuming that obj.objObject.Name is a typo in your reply and that the code in your OP is accurate, then what happens if you put:

On Error Resume Next

On the line immediately abovr the line that is erroring and put:

On Error Goto 0

On the line immediately after the line that is erroring.

(in reply to bkhsms)
 
 
Post #: 4
 
 RE: lastLogonTimestamp - 7/20/2005 7:53:33 AM   
  ehvbs

 

Posts: 2065
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
If you want to deal with the Excel part without (much) further ado,
I propose this approach, based upon a sugestion of rheitzman:

  I'd suggest you just write the information out to a file at a  known location in
  CSV format. The data can be opened directly with Excel or quickly imported into
  a database.
           (http://www.visualbasicscript.com/tm.aspx?m=23809)

Add to your script:

End if
---
Dim oTS
Set oTS = CreateObject( "Scripting.FileSystemObject" )_
          .CreateTextFile( "<YourFSpecHere>", True )
oTS.WriteLine """UserName"";""LogonTimeStamp"""          
---
'Get the user info in the base DN

If intLLTS < intReqCompare Then
               wscript.echo objUser.Name
               wscript.echo objUser.lastLogonTimeStamp
--
   oTS.WriteLine """" & objUser.Name & """;""" & objUser.lastLogonTimeStamp & """"          
--               
End If

End If
Next
----
oTS.Close

Open <YourFSpecHere> with Excel (as CSV)

Because I didn't test this, be prepared for errors.

(in reply to bkhsms)
 
 
Post #: 5
 
 RE: lastLogonTimestamp - 7/20/2005 8:06:36 AM   
  bkhsms

 

Posts: 7
Score: 0
Joined: 7/8/2005
Status: offline
ebgreen:  syntax error = back to the drawing board...thank you.
ehvbs:  i will definitely import this into the remainder of the code.  thanks very much.

(in reply to ehvbs)
 
 
Post #: 6
 
 RE: lastLogonTimestamp - 7/20/2005 8:17:23 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
On Error Resume Next gave you a syntaxt error? Could you post the lines of code that you used?

(in reply to bkhsms)
 
 
Post #: 7
 
 RE: lastLogonTimestamp - 7/20/2005 8:17:27 AM   
  Xandros

 

Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
Bkhsms, you might want to try this script as an alternate base from which to start.  Unless you are running Active Directory 2003 your script will fail (it might anyway due to the very last "wscript.echo" statement... try echoing the "intLLTS" variable instead).  The propertyname "lastlogonTimeStamp" is new in 2003.  The almost-equivalent in earlier releases of AD is "lastlogon" (which also still exists in 2003).  However, there are two other potential gotcha's... If you have multiple domain controllers in your forest only 2003 replicates the value of "lastlogonTimeStamp" to each DC ("lastlogon" is not replicated... go figure).  Also, the replication is only guaranteed to occur at least every 14 days.  Both of these "features" means that you might not obtain the most accurate info.

The only safe work-around in a multi-DC environment is to query each of the DC's and use the most recent value regardless of which AD version you have.

Here is a working script (I've coded for 2003 and earlier but only tested it in my single-DC Win 2000 AD environment).


Option Explicit

Const ADS_SCOPE_SUBTREE = 2

Dim objRootDSE
Dim objConnection, objCommand, objRecordSet
Dim UserDN, objUser, strDNSDomain, strQuery
Dim objLogon, strWeeks, strDays, intLogonTime
Dim intLLTS, intReqCompare, ADVersion

'ADVersion = "2003"
ADVersion = "2000"

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

' Use ADO to search Active Directory for all Users.
Set objConnection      = CreateObject("ADODB.Connection")
Set objCommand         = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

strQuery = "SELECT distinguishedName FROM 'LDAP://" & strDNSDomain & "' WHERE objectCategory = 'User'"

objCommand.CommandText = strQuery
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
   UserDN = objRecordSet.Fields("distinguishedName").Value
   Set objUser = GetObject("LDAP://" & UserDN)

   ' Begin calculation
   If ADVersion = "2003" Then
      set objLogon = objUser.Get("lastLogonTimeStamp")
   Else
      set objLogon = objUser.Get("lastLogon")
   End If
   intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
   intLogonTime = intLogonTime / (60 * 10000000)
   intLogonTime = intLogonTime / 1440
   intLLTS = intLogonTime + #1/1/1601#
   strDays = strWeeks * 7
   intReqCompare = Now - strDays
   If intLLTS < intReqCompare Then
                  wscript.echo Mid(objUser.Name,4) & " last logged on at " & intLLTS
   End If

   objRecordSet.MoveNext
Loop

(in reply to ehvbs)
 
 
Post #: 8
 
 RE: lastLogonTimestamp - 7/20/2005 8:32:47 AM   
  Xandros

 

Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
Minor problem with my example script I just posted... since strWeeks was 0, it lists all users.  Setting strWeeks to a legitimate value produces expected results... except for users who have NEVER logged on.  You might want to address that cosmetically so it reflects NEVER instead of 1/1/1601.

Lynn

(in reply to Xandros)
 
 
Post #: 9
 
 RE: lastLogonTimestamp - 7/21/2005 1:09:38 AM   
  bkhsms

 

Posts: 7
Score: 0
Joined: 7/8/2005
Status: offline
I set it using the inputbox to ask the user when they'd like to compare:

Input1 = InputBox("Enter the number of weeks to query","Last Logon Timestamp")
              If Input1 = "" Then
              MsgBox "You must enter a valid integer", 64, "Alert"
              End if

(in reply to Xandros)
 
 
Post #: 10
 
 RE: lastLogonTimestamp - 7/25/2005 6:57:49 AM   
  bkhsms

 

Posts: 7
Score: 0
Joined: 7/8/2005
Status: offline
This is the script as it is progressing thus far.  With all of your help we've gotten this to the point where it outputs the results based on querying AD 2003 on the lastLogonTimeStamp attribute.  The script fails if it runs across a user object that does not have this value set.  I've countered this by just entering an "on error resume next".  The problem is that my results are getting multiple hits for one user and at the same time one hit (as it should be) for others.  I've bastardized some other scripts here to come up with the end result.  Could someone do a sanity check for me and point out what could be returning multiple hits for one user object?  Thanks:

Const ADS_SCOPE_SUBTREE = 1
Const ForWriting = 2
Const ForAppending = 8
     
Dim objRootDSE
Dim objConnection, objCommand, objRecordSet
Dim UserDN, objUser, strDNSDomain, strQuery
Dim objLogon, strWeeks, strDays, intLogonTime
Dim intLLTS, intReqCompare, ADVersion
Dim oXLS : Set oXLS = WScript.CreateObject("Excel.Application")

'Configure Excel while leaving the spreadsheet hidden
oXLS.WorkBooks.Add
oXLS.Columns(1).ColumnWidth = 20
oXLS.Columns(2).ColumnWidth = 10
oXLS.Columns(3).ColumnWidth = 20

'Set column headers
oXLS.Cells(1, 1).Value = "User Name"
oXLS.Cells(1, 2).Value = "User ID"
oXLS.Cells(1, 3).Value = "Last Logon Date"

'Format text (bold)
oXLS.Range("A1:C1").Select
oXLS.Selection.Font.Bold = True
oXLS.Selection.Interior.ColorIndex = 1
oXLS.Selection.Interior.Pattern = 1 'xlSolid
oXLS.Selection.Font.ColorIndex = 2

'Left Align text
oXLS.Columns("B:B").Select
oXLS.Selection.HorizontalAlignment = &hFFFFEFDD ' xlLeft
ADVersion = "2003"
'ADVersion = "2000"
' Determine DNS domain name from RootDSE object.
'Set objRootDSE = GetObject("LDAP://RootDSE")
'strDNSDomain = objRootDSE.Get("defaultNamingContext")
Input1 = InputBox("Enter the root DSE to query","Last Logon Timestamp")
       If Input1 = "" Then
       MsgBox "You must enter a valid DSE", 64, "Alert"
       End if
strDNSDomain = Input1

'Number of weeks to check
Input2 = InputBox("Enter the number of weeks to check","Last Logon Timestamp")
      If Input2 = "" Then
      MsgBox "You must enter a valid integer", 64, "Alert"
      End If
strWeeks = Input2

' Use ADO to search Active Directory for all Users.
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
strQuery = "SELECT distinguishedName FROM 'LDAP://" & strDNSDomain & "' WHERE objectCategory = 'User'"
objCommand.CommandText = strQuery
Set objRecordSet = objCommand.Execute

intIndex = 2
i = 0
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
UserDN = objRecordSet.Fields("distinguishedName").Value
Set objUser = GetObject("LDAP://" & UserDN)
'Begin calculation
If ADVersion = "2003" Then
'On error Resume Nex
    set objLogon = objUser.Get("lastLogonTimeStamp")  <--ERROR if LLTS attrib not set
Else
    set objLogon = objUser.Get("lastLogon")
End If
intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
intLogonTime = intLogonTime / (60 * 10000000)
intLogonTime = intLogonTime / 1440
intLLTS = intLogonTime + #1/1/1601#
strDays = strWeeks * 7
intReqCompare = Now - strDays
If intLLTS < intReqCompare Then
      strUser = Mid(objUser.Name,4)
      strSamAcct = objUser.samAccountName
      strDate = IntLLTs
End If
call Show(strUser, strSAMAcct, strDate)
i = i + 1
objRecordSet.MoveNext
Loop

'Make the spreadsheet visable
oXLS.Visible = TRUE
Sub Show(strUser, strSAMAcct, strDate)
   oXLS.Cells(intIndex, 1).Value = strUser
   oXLS.Cells(intIndex, 2).Value = strSAMAcct
   oXLS.Cells(intIndex, 3).Value = strDate
   intIndex = intIndex + 1
   oXLS.Cells(intIndex, 1).Select
End Sub

(in reply to bkhsms)
 
 
Post #: 11
 
 RE: lastLogonTimestamp - 7/26/2005 8:32:21 AM   
  krafty

 

Posts: 4
Score: 0
Joined: 7/15/2005
Status: offline
quote:

ORIGINAL: Xandros

Bkhsms, you might want to try this script as an alternate base from which to start.  Unless you are running Active Directory 2003 your script will fail (it might anyway due to the very last "wscript.echo" statement... try echoing the "intLLTS" variable instead).  The propertyname "lastlogonTimeStamp" is new in 2003.  The almost-equivalent in earlier releases of AD is "lastlogon" (which also still exists in 2003).  However, there are two other potential gotcha's... If you have multiple domain controllers in your forest only 2003 replicates the value of "lastlogonTimeStamp" to each DC ("lastlogon" is not replicated... go figure).  Also, the replication is only guaranteed to occur at least every 14 days.  Both of these "features" means that you might not obtain the most accurate info.

The only safe work-around in a multi-DC environment is to query each of the DC's and use the most recent value regardless of which AD version you have.



It is true that this value replicates in 2003 but your forest (I think forest, maybe just domain) has to be in 2003 functional mode. Simply having 2003 DCs isnt enough.

Also, I am not positive that OWA logins are recorded in the lastlogonTimeStamp. That has become an issue for me...one I havent solved yet.

Just some things to think about.

(in reply to Xandros)
 
 
Post #: 12
 
 RE: lastLogonTimestamp - 7/28/2005 2:50:50 AM   
  bkhsms

 

Posts: 7
Score: 0
Joined: 7/8/2005
Status: offline
Krafty - forest and domain is set to 2003 functional level.  I read a little further and found that if the object has never been logged on then of course that attrib won't be set.  Or if you have transferred the user object from 2K, then that attrib won't be set until logon.  I'm aware of the replication issue concerning the lastLogon attrib not being replicated.  Now I'm having more probs in that the LLTS attrib isn't being replicated on some objects and the script is picking up on that.  Yay.

(in reply to krafty)
 
 
Post #: 13
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> lastLogonTimestamp Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts