Moderators: Flying Fox, Ryu Connor
Basically, yes. We also need to track who executed the exe file.Master Kenobi wrote:Do you simply want to log each time one of your users executes a certain EXE file?
Master Kenobi wrote:Simple enough. What operating systems need to be supported?
Option Explicit
Dim WshShell, objFSO, WshNetwork
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Dim strComputer, strProcess, strOutputFile, objFile, strComputerName
Dim strStart, strStartLast, strQuery, X, objWMIService, colProcess, objProcess, strTemp, strUser, strDomain, intRet
'Specify the EXE file to monitor
strProcesss = "cmd.exe"
'Specify the outpue file, it assumes it will be a CSV for easy loading into MSExcel or other Spreadsheet solution.
strOutputFile = "processlog.csv"
'Variable initialization
strComputerName = WshNetwork.ComputerName
strComputer = "." 'Local Computer
strStart = 0
If Not objFSO.FileExists(strOutputFile) Then
objFSO.CreateTextFile(strOutputFile)
Set objFile = objFSO.OpenTextFile(strOutputFile, 8)
objFile.WriteLine("UserName,ExecuteDateTime,ComputerName")
objFile.Close
End If
X = 1
Do Until X = 0 'Keeps the script running until the user shuts down their computer
ProcessCheck()
WScript.Sleep(60000) 'Re-check every 60 seconds
Loop
Sub ProcessCheck
strQuery = "Select * from Win32_Process where name = '" & strProcess & "'"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If objWMIService.ExecQuery(strQuery).Count > 0 Then
Set colProcess = objWMIService.ExecQuery(strQuery)
For Each objProcess in colProcess
strStart = objProcess.CreationDate
strTemp = Split(strStart, ".")
strStart = strTemp(0)
If strStart > strStartLast Then
intRet = objProcess.GetOwner(strUser, strDomain)
Log()
strStartLast = strStart
End If
Next
End If
End Sub
Sub Log
Set objFile = objFSO.OpenTextFile(strOutputFile, 8)
objFile.WriteLine(strUser & "," & strStart & "," & strComputerName)
objFile.Close
End Sub
Master Kenobi wrote:VBScript is uncompiled and handles everything at execution. Simply copy/paste the code into notepad and give the file an extension of VBS.
Example: SoftwareMeter.vbs and double click to start execution much like an EXE file.

Master Kenobi wrote:A valid point. I'm not sure from the information he listed if they are currently using a shared login or if they want to go to a shared one based on the usage of the application in question. There are other ways to differentiate the users, like if they map a network share drive to a unique credential I could pull that as well. Another option is if they use email, tie it into their email credentials. Or an even simpler method would be simply to ask the user following the login what their name is. Generally I prefer not to get information from a user whenever possible though.

Flying Fox wrote:So there are multiple computers connected to multiple instances of the same set of equipment that you want to track by which lab/computer uses the software? If this is the case then the script would work, you can even log the info to a centralized file as part of your script.
If it is a single computer with multiple lab users that can launch the program, then you need some unique ID mechanism. If not using Windows accounts you have to prompt for it. Problem with your own prompts and your own mechanism is it is prone to spoofing and that you are reinventing the wheel when the OS has this figured out already.
In this case, would it be possible to create a script that prompts the user to select one of the Windows users (just to select from a set of names) and add that to the log?
I assume I could modify the script to then only append the log when the process create/start date/time is different than the last entry in the log.
Option Explicit
Dim WshShell, objFSO, WshNetwork
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Dim strComputer, strProcess, strOutputFile, objFile, strComputerName
Dim strStart, strStartLast, strQuery, X, objWMIService, colProcess, objProcess, strTemp, strUser, strDomain, intRet
Dim strDate, strTime
'Specify the EXE file to monitor
strProcesss = "cmd.exe"
'Specify the output file, it assumes it will be a CSV for easy loading into MSExcel or other Spreadsheet solution.
strOutputFile = "processlog.csv"
'Variable initialization
strComputerName = WshNetwork.ComputerName
strComputer = "." 'Local Computer
strStart = 0
If Not objFSO.FileExists(strOutputFile) Then
objFSO.CreateTextFile(strOutputFile)
Set objFile = objFSO.OpenTextFile(strOutputFile, 8)
objFile.WriteLine("UserName,ExecuteDate,ExecuteTime,ComputerName,UTC")
objFile.Close
End If
X = 1
Do Until X = 0 'Keeps the script running until the user shuts down their computer
ProcessCheck()
WScript.Sleep(60000) 'Re-check every 60 seconds
Loop
Sub ProcessCheck
strQuery = "Select * from Win32_Process where name = '" & strProcess & "'"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If objWMIService.ExecQuery(strQuery).Count > 0 Then
Set colProcess = objWMIService.ExecQuery(strQuery)
For Each objProcess in colProcess
strStart = objProcess.CreationDate
strTemp = Split(strStart, ".")
strStart = strTemp(0)
If strStart > strStartLast Then
intRet = objProcess.GetOwner(strUser, strDomain)
Log()
strStartLast = strStart
End If
Next
End If
End Sub
Sub Log
Set objFile = objFSO.OpenTextFile(strOutputFile, 8)
strDate=CDate(Mid(strStart,5,2) & "/" & Mid(strStart,7,2) & "/" & Left(strStart,4))
StrTime=CDate(Mid(strStart,9,2) & ":" & Mid(strStart,11,2) & ":" & Mid(strStart,13,2))
objFile.WriteLine(strUser & "," & strDate & "," & strTime & "," & strComputerName & "," & strStart)
objFile.Close
End SubAlso, is there a way to state the directory I want the log file created in?
strOutputFile = "C:\Program Files\Stuff\processlog.csv"strOutputFile = "\\server1\test\Stuff\processlog.csv"Option Explicit
Dim WshShell, objFSO, WshNetwork
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Dim strComputer, strProcess, strOutputFile, objFile, strComputerName
Dim strStart, strStartLast, strQuery, X, objWMIService, colProcess, objProcess, strTemp,
strUser, strDomain, intRet
Dim strDate, strTime
'Specify the EXE file to monitor
strProcess = "cmd.exe"
'Specify the output file, it assumes it will be a CSV for easy loading into MSExcel or other
Spreadsheet solution.
strOutputFile = "processlog.csv"
'Variable initialization
strComputerName = WshNetwork.ComputerName
strComputer = "." 'Local Computer
strStart = 0
If Not objFSO.FileExists(strOutputFile) Then
objFSO.CreateTextFile(strOutputFile)
Set objFile = objFSO.OpenTextFile(strOutputFile, 8)
objFile.WriteLine("UserName,ExecuteDate,ExecuteTime,ComputerName,UTC")
objFile.Close
End If
X = 1
Do Until X = 0 'Keeps the script running until the user shuts down their computer
ProcessCheck()
WScript.Sleep(60000) 'Re-check every 60 seconds
Loop
Sub ProcessCheck
strQuery = "Select * from Win32_Process where name = '" & strProcess & "'"
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" &
strComputer & "\root\cimv2")
If objWMIService.ExecQuery(strQuery).Count > 0 Then
Set colProcess = objWMIService.ExecQuery(strQuery)
For Each objProcess in colProcess
strStart = objProcess.CreationDate
strTemp = Split(strStart, ".")
strStart = strTemp(0)
If strStart > strStartLast Then
intRet = objProcess.GetOwner(strUser, strDomain)
Log()
strStartLast = strStart
End If
Next
End If
End Sub
Sub Log
Set objFile = objFSO.OpenTextFile(strOutputFile, 8)
strDate=CDate(Mid(strStart,5,2) & "/" & Mid(strStart,7,2) & "/" & Left(strStart,4))
StrTime=CDate(Mid(strStart,9,2) & ":" & Mid(strStart,11,2) & ":" & Mid(strStart,13,2))
objFile.WriteLine(strUser & "," & strDate & "," & strTime & "," & strComputerName & "," &
strStart)
objFile.Close
End SubMaster Kenobi wrote:Also, is there a way to state the directory I want the log file created in?
Just change the value here, the value is set up to be a path/file. It can be something like this or this.
I noticed a typo in one of the variables. I've corrected it in this code piece. But otherwise, yes your modification should work, it tested fine on my system here.
Users browsing this forum: No registered users and 5 guests