Custom Search

Mar 20, 2009

Capture login time / logout time for users on any Windows PC using simple scripts

The following scripts can be used to track user login/logout activity on workstations or terminal servers. I have it set up to track just one PC, but you could easily adapt this script to monitor multiple workstations by removing the IF statement after "Set THENetwork . . . " line. If you do this, please do not forget to remove the "End IF" statement as well.

You need to personalize the BOLD/ITALIC statements to your environment.

I used a GPO to run these scripts at logon and logoff in our domain. I suggest you do the same. You need to create a network location that is accessible by everyone. You can set the folder permission to allow write but only allow administrators to list/read the location.

The output files are Comma Separated and can be easily opened by Microsoft Excel.


LoginReport.vbs

START SOURCE CODE

WriteLineToFile()

Function WriteLineToFile
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set THENetwork = WScript.CreateObject("WScript.Network")
IF (THENetwork.ComputerName = "CHANGE THIS TO THE COMPUTER YOU ARE MONITORING") Then
filename = "OUTPUT LOCATION" & THENetwork.UserName & ".csv"
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename, ForAppending, True)
qMark = Unescape("%22")
coma = Unescape("%2C")
today = date()
curTime = Time
username = THENetwork.UserName
computername = THENetwork.ComputerName
f.Writeline()
f.Write qMark & today & qMark & coma
f.Write qMark & username & qMark & coma
f.Write qMark & computername & qMark & coma
f.Write qMark & curTime & qMark & coma
Set f = fso.OpenTextFile(filename, ForReading)
WriteLineToFile = f.ReadAll
End IF
End Function

END SOURCE CODE



logoutReport.vbs

START SOURCE CODE

WriteLineToFile()

Function WriteLineToFile
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set THENetwork = WScript.CreateObject("WScript.Network")
IF (THENetwork.ComputerName = "CHANGE THIS TO THE COMPUTER YOU ARE MONITORING") Then
filename = "OUTPUT LOCATION" & THENetwork.UserName & ".csv"
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename, ForAppending, True)
qMark = Unescape("%22")
coma = Unescape("%2C")
today = date()
curTime = Time
username = THENetwork.UserName
computername = THENetwork.ComputerName
f.write qMark & curTime & qMark & coma
Set f = fso.OpenTextFile(filename, ForReading)
WriteLineToFile = f.ReadAll
End IF
End Function

END SOURCE CODE







Please leave me comments. Let me know if this worked for you, or if you notice something that needs to be changed. Use the Tell&Share icon under the title of the post to share it with your friends or digg it!

4 comments:

  1. Hi,

    I want to write this login logoff timing code in VB6.0 .
    Could you pls help me out in this?

    ReplyDelete
    Replies
    1. I'm sorry, I am not a VB guy. You'll need to get a book.

      Delete
  2. excellent script man!!!

    have deployed it on my network through gpo and works like a charm!!!!!!!!!!!!!!!!!!!!!

    ReplyDelete