|
Last Logon Export By Organizational Unit |
|
|
|
Written by Carlton Colter
|
|
Thursday, 15 June 2006 00:00 |
|
I can't remember where I found the code to format the logon time. If I could, I'd give them the credit. This script searches ad to export the last logon time for each user. If you're trying to get the logon time of a particular user then you should register the acctinfo.dll from the Windows Resource Kit and the information will be viewable from Active Directory.
With some minor modifications, it can be scheduled to run daily using scheduled task, then run to export to a folder.
Script Code:
'Last Logon Exporter by OU
Set FS = CreateObject("Scripting.FileSystemObject")
Dim TXT_LASTLOGON
Set TXT_LASTLOGON = FS.OpenTextFile("LastLogon.txt", 2, True)
Function FormatLogonDate (objDate)
On Error Resume Next
If Err.Number <> 0 Then
On Error GoTo 0
dtmDate = #1/1/1601#
Else
On Error GoTo 0
lngHigh = objDate.HighPart
lngLow = objDate.LowPart
If lngLow < 0 Then
lngHigh = lngHigh + 1
End If
If (lngHigh = 0) And (lngLow = 0 ) Then
dtmDate = #1/1/1601#
Else
dtmDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _
+ lngLow)/600000000 - lngBias)/1440
End If
End If
FormatLogonDate = dtmDate
End Function
sub SearchOU (oOU, LogFile)
oOU.Filter = Array("User")
For Each oUser in oOU
'The below line filters out computers... imagine that....
If Right(oUser.SAMAccountName,1)<>"$" Then
ExportUserLoginTime oUser, LogFile
End If
Next
oOU.Filter = Array("OrganizationalUnit")
For Each oSubOU in oOU
SearchOU oSubOU, LogFile
Next
End Sub
Sub ExportUserLoginTime (oUser, LogFile)
ON ERROR RESUME NEXT
Set oLogon = oUser.Get("lastLogon")
iLogonTime = #1/1/1601#
iLogonTime = FormatLogonDate(oLogon)
Set oPWSet = oUser.Get("pwdLastSet")
iPWSetTime = #1/1/1601#
iPWSetTime = FormatLogonDate(oPWSet)
LogFile.WriteLine oUser.UserPrincipalName & vbtab & oUser.cn & vbtab & _
iLogonTime & vbtab & iPWSetTime
ERR.CLEAR
ON ERROR GOTO 0
End Sub
Dim ROOTOU
ROOTOU = "LDAP://FQDN-DOMAIN/DC=Domain,DC=com"
ROOTOU = Inputbox("Please enter the DN for the root of the domain:", _
"ROOT DISTINGUISHED NAME", ROOTOU)
Dim oRootOU
set oRootOU = GetObject(ROOTOU)
TXT_LASTLOGON.WriteLine "Useranem" & vbtab & "User" & vbtab & _
"Logon Time" & vbtab & "Password Set Time"
SearchOU oRootOU, TXT_LASTLOGON
TXT_LASTLOGON.Close
MsgBox "COMPLETED!",48,"LAST LOGON EXPORT"
|
|
Last Updated ( Monday, 07 July 2008 13:42 )
|
0 Comments