Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBscript for logon/logoff date time stamp? 1

Status
Not open for further replies.

Askalon

Technical User
Nov 27, 2006
20
US
Currently our system uses a vbscript file to map drives and printers when you login. We are trying to track when users login and logoff thier workstations or the terminal server. Is there an easy way to do this? I know I can add a logoff script through group policy, but I am unsure how to write the script itself. I would prefer that it go to a central folder and append to seperate files with the username as the file name in a txt format (ie: clayton.txt).

Any and all help would be appreaciated.

Thank You

Clayton
 
well theres two options...

1 is to use simple commands to write to a log file (even something as simple as the date/time could meet your goals potentially if using single files...

there is a thread containing the commands in the win2000 or win2003 forum...

-Brandon Wilson
MCSE00/03, MCSA:Messaging00, MCSA03, A+

 
This is what i use/do. I created 2 scripts, i put them in the default domain policy so it catches all logons and logoffs. It actually inserts the data i want into a sql table i created so i could write a crystal report to get the info i wanted in a format i wanted. The info i collect is date and time, the username, the workstation name, and the action either logon or logoff.

This goes in the logon script section.
Code:
ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, ComputerString, UserNameString, DateTimeString, objConn, objRSSQL, actionString

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")

'Database Connection String
Set objConn = CreateObject("ADODB.Connection")
    objConn.Open "Driver={SQL Server};" & _
                 "Server=yourSQLserver;" & _
                 "Database=yourdatabasename;" & _
                 "user id=youruserid;" & _
                 "password=youruseridpassword;"
Set objRSSQL = CreateObject("ADODB.Recordset")

'Get Date and Time, Workstation Name, and User Name
ComputerString = WshNetwork.ComputerName 
UserNameString = WshNetwork.UserName
DateTimeString = now()
actionString = "Logon"

'Insert info in to database
SQLQuery = "insert into LogonTracking (logindatetime, logonworkstation, logonuser, action)" & _
              " values ('" & DateTimeString & "','" & ComputerString & "','" & UserNameString & "','" & actionString & "')"
    objRSSQL.Open SQLQuery, objConn

'Clean up Memory
set WSHShell = nothing
set WSHNetwork = nothing
set ComputerString = nothing
set UserNameString = nothing
set DateTimeString = nothing
set actionString = nothing
set objConn = nothing
set objRSSQL = nothing

'Quit the Script
wscript.quit

This goes in the logoff script section.
Code:
ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, ComputerString, UserNameString, DateTimeString, objConn, objRSSQL, actionString

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")

'Database Connection String
Set objConn = CreateObject("ADODB.Connection")
    objConn.Open "Driver={SQL Server};" & _
                 "Server=yourSQLserver;" & _
                 "Database=yourdatabasename;" & _
                 "user id=youruserid;" & _
                 "password=youruseridpassword;"
Set objRSSQL = CreateObject("ADODB.Recordset")

'Get Date and Time, Workstation Name, and User Name
ComputerString = WshNetwork.ComputerName 
UserNameString = WshNetwork.UserName
DateTimeString = now()
actionString = "Logoff"

'Insert info in to database
SQLQuery = "insert into LogonTracking (logindatetime, logonworkstation, logonuser, action)" & _
              " values ('" & DateTimeString & "','" & ComputerString & "','" & UserNameString & "','" & actionString & "')"
    objRSSQL.Open SQLQuery, objConn

'Clean up Memory
set WSHShell = nothing
set WSHNetwork = nothing
set ComputerString = nothing
set UserNameString = nothing
set DateTimeString = nothing
set actionString = nothing
set objConn = nothing
set objRSSQL = nothing

'Quit the Script
wscript.quit



RoadKi11

"This apparent fear reaction is typical, rather than try to solve technical problems technically, policy solutions are often chosen." - Fred Cohen
 
RoadKi11, would I be able to do this MySql as well? just change the driver to MySql or leave it alone?
 
No reason it wouldnt work with mysql, you would have to tweak the connection string and maybe the insert string, not sure to what though been a long time sinse i worked with mysql. You could keep this totally seperate also, this will work with MSDE 2000 and MSSQL 2005 Express both of which are free MS products.



RoadKi11

"This apparent fear reaction is typical, rather than try to solve technical problems technically, policy solutions are often chosen." - Fred Cohen
 
WOHOO!
I installed SQL express and finally got it going. I did not allow enough room in the field (it se t it to 10 by default). Now I am rocking.

Thanks Roadki11

 
You are welcome, glad i could contribute.



RoadKi11

"This apparent fear reaction is typical, rather than try to solve technical problems technically, policy solutions are often chosen." - Fred Cohen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top