Additional Info for this topic by Don Jones. I am looking at using his method to update what I have in place with my smaller clients.
Windows Can’t Do It—Building Your Own Logon Tracking
Click PopUp to download Word file with VBS code.
I found the code that we had developed that would ask for a reason that a user is logging into a Server / PC and it would record the Date, Time, UserName, ServerName (or PCName).
1/23/2010 7:54:47 PM
TimBolton : Tims-PC
There are two Pop-Up windows that will appear. Regardless if the user enters any information or not, it will update the Activity.log file with their Date, Time, UserName, ServerName (or PCName).
If more information needs to be submitted for documentation purposes, a second window can be used. The user can select either Yes or No at this time.
If the user selects Yes, the Activity.log file opens allowing the user to enter the applicable information.
The second window can be taken out of the code if you find that necessary and more secure settings can be applied to the Activity.log file as needed.
This helped me out a great deal while working in a test lab that had numerous office personnel logging into my servers. I was able to track who logged in while trouble shooting issues that occurred at a later time. You would be surprised how someones memory will come back to them, when you can prove they were the last ones to log onto a server…
This is the VBS code.
Dim objNetwork, objFSO, objFile, objShell
Dim strLogFile, strLogLine, strOneLiner, strRunCommand
Dim strDate, strUser, strComputer
Dim bAsk4Comment, bAsk2OpenLog, bSuccess
Dim x
‘ ===================================================
‘ PREFERENCES
‘ Full path to central log file
‘ strLogFile = “\\Server\ShareName\Activity Log\Activity.log”
strLogFile = “C:\BVS\Activity.log”
‘ Ask user for a one-line comment? Set to False to disable
bAsk4Comment = True
‘ Ask user to open the log file? Set to false to disable
bAsk2OpenLog = True
‘ END OF PREFERENCES
‘ ===================================================
Set objNetwork = WScript.CreateObject(“WScript.Network”)
‘ Get logon information
strDate = Now()
strUser = objNetwork.UserName
strComputer = objNetwork.ComputerName
‘Get a one-line comment from user
strOneLiner = “”
If bAsk4Comment = True Then
strOneLiner = InputBox(“Enter a short comment to go in the server change log:”,”Reason for your logon?”)
End If
‘ Compile text that will be written to log
strLogLine = vbCRLF
strLogLine = strLogLine & strDate & vbCRLF
strLogLine = strLogLine & strUser & ” : ” & strComputer & vbCRLF
‘ If a one-line comment was specified, add it here.
If strOneLiner <> “” Then
strLogLine = strLogLine & strOneLiner
End If
‘ Append text to log file
Const ForAppending = 8
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
If not objFSO.FileExists(strLogFile) Then
Set objFile = objFSO.CreateTextFile(strLogFile)
objFile.Close
End If
Set objFile = objFSO.OpenTextFile(strLogFile, ForAppending)
objFile.WriteLine strLogLine
objFile.Close
‘ Throw up MessageBox asking to open the log file
If bAsk2OpenLog = True Then
x = MsgBox(“Would you like to open the activity log for further comments?”,4,”Open Log?”)
‘ If they clicked Yes…
If x = 6 Then
Set objShell = CreateObject(“WScript.Shell”)
strRunCommand = “notepad ” & strLogFile
‘ Run notepad, don’t wait for it to close
objShell.Run strRunCommand,1,False
‘ Bring Notepad to the foreground…keep trying…
Do Until bSuccess = True
bSuccess = objShell.AppActivate(“notepad”)
Wscript.Sleep 250
Loop
‘ send a CTRL-END to get us to the bottom of the log.
objShell.SendKeys “^{END}”
End if
End If
——————– OLD CODE ——————–
For ease of explanation I placed this onto the local C:\ drive of my laptop, but you can use a shared drive for the “HistoryFile.txt” file which you can also set to hidden.
This will create a Pop-Up window to appear when anyone logs into a Server / PC and ask them to enter a reason as to what they are doing and why, I entered “Test”. This is similar to the window that appears when you log off of a server or if a server is re-booted for unknown reasons.
NOTE* – The user must select a reason to enter, which will enter their logon information (Time, Name of Server / PC, UserName, Name FROM Server / PC) that will be posted to the txt file.
I am working on a change that will enter the users logon info even if they hit the cancel button.
Example -
1/23/2010 12:41:08 PM - Tims-PC\Tim – Tims-PC
- Test
I created this BAT file and saved it on the C:\ drive.
@echo off
Also located on the C:\ drive is this VBS file “PopUp.vbs”.
Option Explicit
‘ Const txtfile = “\\Server\Share\HistoryFile.txt”
‘ Const txtfile = “\\Server\Share\HistoryFile.txt”
Const txtfile = “C:\HistoryFile.txt”
Const sleepsecs = 5
Dim wshShell, WshEnv, strMsg, prmpt, fso, logfile, retopen, descopen
prmpt = “Enter message:”
Do
strMsg = InputBox(prmpt, “Message?”, ” “)
If strMsg <> “” Then Exit Do
prmpt = “Message cannot be blank!” & vbCrLf & “Enter message:”
Loop
Set WshShell = WScript.CreateObject(“WScript.Shell”)
Set WshEnv = WshShell.Environment(“Process”)
Set fso = CreateObject(“Scripting.FileSystemObject”)
On Error Resume Next
Err.Clear
Set logfile = fso.OpenTextFile(txtfile,8,True,0)
retopen = Err.Number
descopen = Err.Description
On Error GoTo 0
Do While retopen <> 0
If Msgbox(“Log file ‘” & txtfile & “‘ cannot be opened (” & descopen & “)” & vbCrLf & _
“Sleep ” & sleepsecs & ” and try again? (OK) or terminate? (Cancel)”,49,”Log file cannot be opened.”) = vbCancel Then
wscript.quit 4
End If
WScript.Sleep(sleepsecs * 1000)
On Error Resume Next
Err.Clear
Set logfile = fso.OpenTextFile(txtfile,8,True,0)
retopen = Err.Number
On Error GoTo 0
Loop
logfile.WriteLine(Now() & ” – ” & wshEnv(“USERDOMAIN”) & “\” & WshEnv(“USERNAME”) & ” – ” &WshEnv(“COMPUTERNAME”))
logfile.WriteLine(“”)
logfile.WriteLine(” – ” & strMsg)
logfile.WriteLine(“”)
logfile.WriteLine(“”)
logfile.Close
wscript.quit 0
You can place the BAT file in the “ALL USERS” Startup folder. Another option is to create a separate script for each server, but I have found that it’s easier to search through a single txt file for a name and / or server.





MCITP – MCTS