Re: VB Script - Last Logon
- From: "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 6 Nov 2007 09:22:26 -0600
"RC" <RichJChristy@xxxxxxxxx> wrote in message
news:1194358451.234930.192270@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
This script will give me the Last Logon Time Stamp for Computer
Objects. I want to send the result from the script to a txt or csv
file. How would I do this? Right now it just sends a box to my
desktop.
I also want to make sure it is checking all Domain Controllers to see
who has the lastest time stamp (depending on which DC the user
authenticated with when logging into the domain). I believe Microsoft
shortend the time it takes to replicate this data to all DC's down to
7 days in Windows 2003.
Thanks in advance.
======================================================================
FindOldComputers(90)
WScript.Echo
strDate = GetDate(strDaysOld)
strLDAP = "(&(objectCategory=computer)(lastLogonTimeStamp<=" &
strDate & "))"
set oRootDSE = GetObject("LDAP://RootDSE")
strDomainNC = oRootDSE.Get("defaultNamingContext")
set oRootDSE = Nothing
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Provider = "ADsDSOObject"
oConnection.Open "Active Directory Provider"
Set oCommand = CreateObject("ADODB.Command")
Set oCommand.ActiveConnection = oConnection
strQuery = "<LDAP://"& strDomainNC &">;" & strLDAP &
";AdsPath;subtree"
oCommand.CommandText = strQuery
oCommand.Properties("Page Size") = 1000
Set oRecordSet = oCommand.Execute
if not oRecordSet.Eof Then
WScript.Echo "- Object Count: " & oRecordSet.RecordCount
While Not oRecordSet.Eof
Set x =
GetObject(oRecordSet.Fields("AdsPath").Value)
strTimeStamp = GetLastLogonDate(x.distinguishedName)
WScript.Echo x.name & " - " & strTimeStamp
oRecordSet.MoveNext
Wend
end If
End Sub
'Returns number of 100 nano second intervals starting from 00:00
1/1/1601 minus the days provided.
Function GetDate(strDaysOld)
On Error Resume next
dtmDate = DateAdd("d", -strDaysOld, Now())
dbl100NanoSecs = 10000000 * (DateDiff("s", "1/1/1601", dtmDate))
dbl100NanoSecs = FormatNumber(dbl100NanoSecs, 0, False, False ,
0)
GetDate = dbl100NanoSecs
End Function
'Returns last logon date of provided computer
Function GetLastLogonDate(strCompDN)
'On Error Resume next
set objComp = GetObject("LDAP://" & strCompDN)
set objLogon = objComp.Get("lastLogonTimestamp")
intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
intLogonTime = intLogonTime / (60 * 10000000)
intLogonTime = intLogonTime / 1440
GetLastLogonDate = intLogonTime + #1/1/1601#
End Function
======================================================================
The whole point of retrieving the lastLogonTimeStamp attribute is so you
don't need to query every DC in the domain. The lastLogon attribute is
updated on the DC that authenticates the object, but is never replicated (so
a different value is saved on every DC for the object). The
lastLogonTimeStamp attribute is only updated if the previous value is more
than 14 days (by default) in the past, but it replicates like most other
attributes. Any value for lastLogonTimeStamp that corresponds to a date more
than 14 days in the past will be accurate. This should meet almost all
needs.
For a VBScript program that queries all DC's and retrieves the latest
lastLogon value for all users, see this link:
http://www.rlmueller.net/Last%20Logon.htm
You can change the query from users to computers. In VBScript I send the
output to a text file by running the script at a command prompt with the
cscript host and redirecting the output to a text file. For example:
cscript //nologo LastLogon.vbs > report.txt
This assumes you are in the directory where the file LastLogon.vbs is saved.
Otherwise you must include the path to the file. This command creates the
file report.txt in the current directory. The //nologo option suppresses
logo information.
In a VB program I either use the FileSystemObject to write lines to a file,
or use the VB Open and Print commands. For example:
Open "c:\MyFolder\report.txt" For Output As #1
....
Print #1, strText
.....
Close #1
Look up help on Open, Print, and Close.
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
.
- Follow-Ups:
- Re: VB Script - Last Logon
- From: RC
- Re: VB Script - Last Logon
- References:
- VB Script - Last Logon
- From: RC
- VB Script - Last Logon
- Prev by Date: Re: TAB Index and TabStop; sequences
- Next by Date: Re: How do I?? Add computer account to Global Security group
- Previous by thread: VB Script - Last Logon
- Next by thread: Re: VB Script - Last Logon
- Index(es):
Relevant Pages
|