PDA

View Full Version : Getting Lastlogin from ADS (W2K)



GaryB999
12-12-2002, 02:28 AM
Hi All,

I'm having a nightmare getting the following WSH script to return lastlogin values from our active directory.

On error resume Next

Dim oContainer
Dim oUser
Dim Lastlogon

set wshshell = CreateObject("Wscript.Shell")
set fso = CreateObject("Scripting.FileSystemObject")

'Get output filename
LDAPOutput = inputbox("Please state the path and filename for the LDAP Output? [MANDATORY]","LDAP output Details","C:\temp\LDAP_Extract.txt")

If LDAPOutput = "" then
MSGBOX "Exiting as no Output file declared!",vbOKonly
wscript.quit
End If

Set oContainer=GetObject("LDAP://CN=users,DC=colp,DC=co,DC=uk")

counter = 1

GetUsers(oContainer)

'cleanup
Set oContainer = Nothing

Msgbox "Completed compiling all domain users" & vbcr & vbcr & "Compiled " & counter & " records.", vbokonly

Function GetUsers(oObject)

oObject.Filter = Array("user")

For Each oUser in oObject

If oUser.lastlogin <> "" then
Lastlogon = oUser.lastLogin
Else
Lastlogon = "Unavailable"
End If

UserName = oUser.sAMAccountName

If oUser.sn <> "" then
Firstname = oUser.givenName
LastName = oUser.sn
Name = LastName & " " & Firstname
Else
Name = "Undefined"
End If

AppendFile Name, UserName, lastlogon

counter = counter + 1

Next

End Function

Sub AppendFile(oName, oUserName, oLastLogin)

set OutputFile = fso.opentextfile(LDAPOutput, 8, true)

outputfile.writeline oName & "," & oUserName & "," & oLastLogin

outputfile.close

End Sub


The script executes with error 35 'The active directory property cannot be found in the cache' and only returns 590 records from a total of 1750 !! Any suggestions gratefully received.

Festive thanks,

Gary

Torsten Silbersack
01-29-2003, 03:31 AM
Hi ...
You have to set the LogonServer wihtin your LDAP Query. When you try to get the time without setting the Logonserver your Result will bring back wrong Data.

have a look to my solution

Sub ADUser

Set oAdoRecordset = oAdoConnection.Execute _
("<LDAP://" & sLogonSRV & "/" & sDomName & ">;(&(objectClass=user)(sAMAccountName=" & sDetUser & "));ADsPath;Subtree")
sStr = oAdoRecordset.Fields.Item("ADsPath").Value
Set oAdsObj = Getobject(sStr)
Set oUser = GetObject(sStr)
sLLogon = oUser.LastLogin

you then can write the last logon (sLLogon) to a Database for further actions

Bye
Torsten

Torsten Silbersack
01-29-2003, 03:34 AM
oh I forgot ...

The lastlogon is written if the user press enter in the gina and Lastlogon wil not be replicated between DC's, so you have to wrtie the Lastlogon while the User logs in ... the code I sent you is part of my Logonscript in a 15000User AD !

Bye
Torsten