Record User Login in Windows

This VBScript program creates a record with the current date/time and the user name, each time a user logs into a machine. The logfile is created in C:\UserLog.txt. The program adds an entry into the Run key of the Windows Registry which assures that the script is executed during each login.

The purpose of this script is to demonstrate how to write a script which:

'************************************************
' File:    UserLog.vbs (WSH sample in VBScript) 
' Author:  Günter Born
'
' Writes the user name during each login into a
' log record. The script add itself into the Run
' key of the Registry.
'
' In no way shall the author be liable for any
' losses or damages resulting from the use of this
' program. Use AS-IS at your own risk.
'
' The code is the property of the author. You may
' use the code and modify it, as far as this header
' remains intact. Further updates and other samples
' may be found on my site:
' http://www.borncity.de
'************************************************
Option Explicit 

Dim Text
Dim WshNetwork         ' object variable

'# Add script into Run key !!!
'# so it will be executed during each logon
AddRun                 

' Create a new WshNetwork object to access Network properties
Set WshNetwork = WScript.CreateObject("WScript.Network")

Text = date & "  " & time & ": Computer: " & _
      WshNetworK.ComputerName & vbTab
Text = Text & "Domain: " & _
      WshNetworK.UserDomain & vbTab
Text = Text & "User: " & _
      WshNetworK.UserName

AddLog (Text)            ' write log file entry

' remove for silent use !!!!
WScript.Echo Text        ' show result

'#### some Helpers ####
Sub AddRun()
' add the batchfile to launch the script into  
' the Run key of the Registry
DIM Root, key, valname, Command
Dim WshShell

 Root = "HKEY_LOCAL_MACHINE"
 key = "\Software\Microsoft\Windows\CurrentVersion\Run\"
 valname = "UserLog"
 Command = WScript.ScriptFullName
 Set WshShell = WScript.CreateObject("WScript.Shell")
 WSHShell.RegWrite Root+Key+valname, Command, "REG_SZ"
End Sub

Sub AddLog (txt)
Const file1 = "C:\UserLog.txt"   ' Log file name
Const ForAppending = 8           ' Append mode
Dim fso, fi                      ' object variable

' Creates a FileSystemObject object 
 Set fso = CreateObject("Scripting.FileSystemObject")

' open file, force create, if not exists
 Set fi = fso.OpenTextFile(file1, ForAppending, true)

 fi.WriteLine (txt)              ' append log
 Set fi = Nothing
End Sub
'*** End
TIP: Removing the entry from the Registry may be done using the System Policy Editor (remove the entry from the Run key). In Windows 98 you may use the property page AutoStart within the system configuration program.

In WSH 2 I recommend a more simple solution, just use the LogEvent method exposed by the WshShell object. The following code snippet demonstrates how to create a log file entry:

Option Explicit 
Const INFORMATION = 4  ' value for Log category

Dim Text
Dim WshNetwork         ' object variable
Dim WshShell

' Create a new WshNetwork object to access Network properties
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set WshShell = WScript.CreateObject("WScript.Shell")

Text = date & "  " & time & ": Computer: " & _
      WshNetworK.ComputerName & vbTab
Text = Text & "Domain: " & _
      WshNetworK.UserDomain & vbTab
Text = Text & "User: " & _
      WshNetworK.UserName

WshShell.LogEvent INFORMATION, Text  ' write log file entry

The LogEvent method writes the text submitted into the Windows NT log file as a new record. The first parameter submitted is a constant defining the log category. This constant may be set in the script's header. In Windows 9x the method writes a WSH.log file in the Windows folder. Addition information about this method may be found in the WSH 2 help.

Back

(c) G. Born