Script to retrieve users last logon time
I need help writing a script to get every user on my networks last logon time. I've read this: http://www.microsoft.com/technet/scriptcenter/topics/win2003/lastlogon.mspx
and I basically need to generalize what it does; i.e., instead of getting 1 user's last logon time, I want every user who hasn't logged on in a year. My friend wrote this for me but I doesn't work like I need it to. Sometimes it returns only 1000 users and sometimes it fails. Any ideas?
and I basically need to generalize what it does; i.e., instead of getting 1 user's last logon time, I want every user who hasn't logged on in a year. My friend wrote this for me but I doesn't work like I need it to. Sometimes it returns only 1000 users and sometimes it fails. Any ideas?
On Error Resume Next<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p> <o:p></o:p> Set objFSO = CreateObject("Scripting.FileSystemObject")<o:p></o:p> Set objFile = objFSO.CreateTextFile("lastlogondate.txt")<o:p></o:p> <o:p></o:p> <o:p></o:p> Const ADS_SCOPE_SUBTREE = 2<o:p></o:p> <o:p></o:p> dtmLogonDate = "6/1/2007"<o:p></o:p> <o:p></o:p> Set objShell = CreateObject("Wscript.Shell")<o:p></o:p> lngTimeZoneBias = objShell.RegRead("HKLM\System\CurrentControlSet\Control\" _<o:p></o:p> & "TimeZoneInformation\ActiveTimeBias")<o:p></o:p> If UCase(TypeName(lngTimeZoneBias)) = "LONG" Then<o:p></o:p> lngFinalBias = lngTimeZoneBias<o:p></o:p> ElseIf UCase(TypeName(lngTimeZoneBias)) = "VARIANT()" Then<o:p></o:p> lngFinalBias = 0<o:p></o:p> For k = 0 To UBound(lngTimeZoneBias)<o:p></o:p> lngFinalBias = lngFinalBias + (lngTimeZoneBias(k) * 256^k)<o:p></o:p> Next<o:p></o:p> End If<o:p></o:p> <o:p></o:p> dtmNewDate = DateAdd("n", lngFinalBias, dtmLogonDate)<o:p></o:p> lngSeconds = DateDiff("s", #1/1/1601#, dtmNewDate)<o:p></o:p> strModifiedLogonDate = CStr(lngSeconds) & "0000000"<o:p></o:p> <o:p></o:p> <o:p></o:p> Set objConnection = CreateObject("ADODB.Connection")<o:p></o:p> Set objCommand = CreateObject("ADODB.Command")<o:p></o:p> objConnection.Provider = "ADsDSOObject"<o:p></o:p> objConnection.Open "Active Directory Provider"<o:p></o:p> Set objCommand.ActiveConnection = objConnection<o:p></o:p> <o:p></o:p> objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE <o:p></o:p> <o:p></o:p> objCommand.CommandText = _<o:p></o:p> "SELECT Name, userPrincipalName, sAMAccountName FROM 'LDAP://dc=***,dc=***,dc=com' WHERE objectClass='user' " & _<o:p></o:p> "AND lastlogon<='" & strModifiedLogonDate & "'" <o:p></o:p> Set objRecordSet = objCommand.Execute<o:p></o:p> <o:p></o:p> objRecordSet.MoveFirst<o:p></o:p> Do Until objRecordSet.EOF<o:p></o:p> objFile.WriteLine objRecordSet.Fields("Name").Value & ", " & objRecordSet.Fields("userPrincipalName").Value & ", " & objRecordSet.Fields("sAMAccountName").Value <o:p></o:p> objRecordSet.MoveNext<o:p></o:p> Loop<o:p></o:p>
0
Comments
Make a login script on the domain controller and apply it to the entire domain. You can echo the login details to a file on the server.
login-time.bat