Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


June 2007

It’s 10:00 P.M.: Do You Know Who’s Logged On?

Systems engineer Michael Dragone’s batch-file solution tracks employee logons by user and computer
RSS
Subscribe to Windows IT Pro | See More Security Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
Main Article    Identity Lifecycle Manager 2007

Download the Code Here

Security problems are typically a small part of Michael Dragone's job as a systems engineer for Titleserv, a title insurance and real estate services firm in Woodbury, New York. But when senior management asked him to devise a way to track employees' network logons and logoffs, Michael was challenged to find a solution that didn't rely on the Active Directory (AD) Security event log, which retains logons, logoffs, privilege assignments, and other events only for a short time until they're overwritten. Michael talked to me about how he fulfilled management's request by using straightforward batch-file scripting to capture logon/logoff data on the clients and move that data into text files and folders for easy retrieval.

What inspired you to create the user logon/logoff solution?
Our internal production system is a Web application based on Microsoft .NET Framework, and it has an internal logging component that tells you when people logged on, when they logged off, what they did, and so on. We had a lot of requests from management to provide a similar capability for our end users' PCs. A manager might want to know, for example, if someone requested overtime pay last month, whether that employee was really working overtime. To confirm this, the manager wanted to be able to find out when someone logged on or whether an employee was working on a particular computer at a particular time. That information is logged in AD, but it goes in the Security event log, which can't retain more than 50MB of data and has other events besides those management wanted to track. It would take a lot of scraping through the logs to find the information management wanted.

So you found an alternative method for capturing the logon information?
Yes. I decided to write a batch-file command that would run on all our clients and servers at system logons and capture logon and logoff information for our Windows XP clients and our servers (Windows Server 2003, Windows 2000 Server, and Microsoft Windows Server 2003 and Windows 2000 Server Terminal Services). When a user logs on, a script runs; at logoff, another script runs. The script appends the logon and logoff information to a text file. The text files contain cumulative information: One line says, for example, "logged out of computer name on" and the date and time. The next time the user logs on, the script writes another line in the text file with the new information. (You can download an excerpt of Michael's script. To do so, go to http://www.windowsitpro.com, InstantDoc ID 95922, and click the Download the Code button.)

Another batch-file script retrieves logon/logoff information for a computer. If we want to know who logged on to a computer, we can check the computer's text file for the user name and the logged-out-on and logged-in-on information. The script copies the text file into one of several folders, depending on which machine the script is running. All the users go in one folder, whereas the logons we track by computer are copied into the folder for the appropriate computer type (e.g., server, client). The scripts are run via our existing Group Policy Objects (GPOs), which are segregated by machine. We use our GPOs in merge loop-back-processing mode, so that all user-configuration settings (including this script) are applied regardless of who logs on.

How does management access the logon/ logoff information?
Right now, management will just ask us (IT), and we'll send them over the text file. It takes us seconds to browse the file share and directories where the files are located. We have only 150 users, so this method is manageable for us. We get a lot of requests from managers saying "I need to know if so and so logged on from home. Did they do any work last night?" So we can check the Terminal Services log and find out, say, that the employee logged on at 10:00 p.m. and logged off five minutes later. We zip up the text files, so we can archive them indefinitely.

Did you look into buying a third-party tool that could provide this type of log information?
I looked at some third-party utilities, and there are a lot of utilities that do event-log stuff. But all I wanted to do was track logons and logoffs; the third-party utilities provided a lot more information than I really needed. I didn't really want to buy a utility just for that one purpose, when with a little work, I could do it for free.

How long did it take you to write the batch file, set up the folders, and test your solution?
It took just a few hours to write the batch-file scripts and test them. It took longer to set up all the directories and make sure that, based on where a computer user is in AD, the appropriate script runs and goes in the appropriate folder—so I don't have client PCs showing up in the servers folder, for example.

Have you noticed any changes in user productivity since you began using the logon/logoff tracking system?
I don't think the solution has changed peoples' work habits; they either have a work ethic or they don't. It just makes IT's life easier. Instead of having to pore through event logs, if somebody wants the information, we can just go right to the correct directory, locate the text file, and email it to them. It saves us a lot of time.

Editor's Note:
Long-time reader and contributor Murat Yildirimoglu pointed out that the scripts used in this article are similar to the scripts Murat published in August 2005 in "Prevent Multiple Logons With GPOs".

End of Article



Reader Comments
I was hoping to get some good info from this article regarding how the script was created. Unfortunately it was much more simple than the one I currently use. I guess it would be a good starting point in tracking the users.
The solution I have runs in much the same way but instead copies the file to a hidden network path, and stored in a CSV delimited file. The file contains several fields which would allow me to sort the data if necessary in an Access or Excel program. I too have it running from the GPO in the Default Domain Policy/User configuration/Windows Settings under LOGON/LOGOFF.

Here is how my bat file looks.
@Echo off
Rem Extract only the last IP address from the list
FOR /F "TOKENS=2* DELIMS=:" %%A IN ('IPCONFIG /ALL ^| FIND "IP Address"') DO FOR %%B IN (%%A) DO SET IPADDR=%%B

Rem Extract the NETBIOS name
FOR /F "TOKENS=2* DELIMS=:" %%A IN ('IPCONFIG /ALL ^| FIND "Host Name"') DO FOR %%B IN (%%A) DO SET netbiosname=%%B

Rem Extract the MAC ADDRESS
FOR /F "TOKENS=2* DELIMS=:" %%A IN ('IPCONFIG /ALL ^| FIND "Physical Address"') DO FOR %%B IN (%%A) DO SET macaddress=%%B

echo S,%DATE%,%TIME%,%username%,%netbiosname%,%ipaddr%,%macaddress% >> n:\Logs\PCs\%netbiosname%.txt

The last line saves the data to the CSV file. The 'S' stands for SignOn, use the same bat file in your logoff but replace the 'S' with an 'O' or whatever to denote sign-off.

With this solution you can look at all of the computers in one directory. I have a scheduled task which runs on the first that creates a directory and moves all of the files there.
Something else this solution solves for me is that I have users who roam the network when they login in order to do their job. A simple COPY statement of all the files and dump it to Access, and you know when and where they last logged in for the month, including our TS users.

I think you'll find this to be a better solution and no more difficult to implement and easier to manage given that the data is stored centrally.
Jake

Jakesty June 07, 2007 (Article Rating: )


Jakesty,
Nice script. Thanks

baronferg June 07, 2007 (Article Rating: )


Jakesty: Thanks for reading the article and taking the time to post your script! It's good to hear what readers like you are doing and different approaches you're taking to solving the problems discussed in our articles. It looks as if at least one person has found your alternative logon/logoff tracking solution helpful. I've passed along your comment to Mike Dragone so that he can respond.
--Anne Grubb, senior editor, Windows IT Pro

AnneG_editor June 11, 2007 (Article Rating: )


Nice script, Jakesty. The beauty of this solution is that you can extend it in whatever way fits your environment. In my case I didn't need to capture as much information as you do. If I were to deploy this solution to a larger userbase I would definately track more information such as IP address.

One nice benefit that I see with using a straight "pre-formatted" text file rather than one formatted in CSV is that I can e-mail the entire text file to a manager without having to go though and parse it first before sending it off.

mdragone June 11, 2007 (Article Rating: )


Thank you, it is just something that develops over time and I have just combined a lot of the info I have found and put it all together in simple scripts like these.

Here is that other script to create the folder and move all of the files into the new folder. This way you'll have a separated list of logins on a per month basis. Just add this bat file to your task list.

FOR %%A IN (%Date%) DO SET Today=%%A

FOR /F "tokens=1-3 delims=/-" %%A IN ('ECHO.%Today%') DO SET SortDate=%%C%%A%%B

md \\server1\public\ipconfig\pclogin\%SortDate%

move \\server1\public\ipconfig\pclogin\*.txt \\server1\public\ipconfig\pclogin\%SortDate%

Jakesty June 18, 2007 (Article Rating: )


Very cool; I will implement tomorrow.

GuruLeeNyc June 20, 2007 (Article Rating: )


Just what i was looking for, thanks

deeuk June 22, 2007 (Article Rating: )


i was just wondering if there was a way in which you could log the activity of the user who was logged on. Anybody know of such a script?

deeuk June 22, 2007 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...

Microsoft Delivers Service Pack 2 Beta 2 for Vista, Server 2008

Microsoft on Tuesday announced the availability of the Beta 2 version of Service Pack 2 (SP2) for Windows Vista and Windows Server 2008. Since both operating systems were developed from the same code base, they have a common servicing structure and thus ...

Windows Live Wave 3 Services Launch Begins

Late Tuesday, Microsoft began rolling out the services portion of its Windows Live Wave 3 launch. The company is shipping an unprecedented number of new and improved services that build off the success of Hotmail and Windows Live Messenger and attempt ...


Security Whitepapers The Impact of Messaging and Web Threats

Why SaaS is the Right Solution for Log Management

Protecting (You and) Your Data with Exchange Server 2007

Related Events How IE7 & The New Extended Validation SSL Certificates Impact Your Site

Top 10 Email Security Challenges and Solutions

Introduction to Identity Lifecycle Manager "2"

Check out our list of Free Email Newsletters!

Security eBooks Spam Fighting and Email Security for the 21st Century

Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

Related Security Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing