System Scripts
Gathering and Displaying system and user information remotely.
Having information about who is logged on to your network, when and where is always handy information to have.
You can use a logon script to query all the windows computers on your network. Getting the same information from Linux computers works a little different, but using the script below as a windows users logon script will put all the information you need into a text file that can then be parsed and displayed in a browser using PHP and html.
The script can be assigned as a logon or logoff script by group policy, or if your network isn't a domain or doesn't use group policy, it can be put in the startup group of the all users profile on each machine. Save the file as logon.vbs. You can use the same file and save as logoff.vbs and change the line “strLogin = Login” to Logoff, and use one when users log on and the other when they log off. Note that if the script is used on computers that are not on a domain, the domain name will shows up as the computer name in the text file.
Option Explicit
Dim objFSO, objFolder, objShell, objTextFile, objFile, WshNetwork
Dim strDirectory, strFile, strText, strDomain, strComputer, strUser, strDate, strLogin
Dim objWMIService, colItems, objItem, strComputer1, strIP, strMac
Dim objWMISvce, strDesc, object
strDirectory = "C:\user_logs$\time"
On Error Resume Next
strComputer1 = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer1 & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For Each objItem in colItems
strMac = objItem.MACAddress(0)
strIP = objItem.IPAddress(0)
Next
Set ObjWMISvce = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer1 & "\root\cimv2").ExecQuery("Select * FROM Win32_OperatingSystem")
for each object in objWMISvce
strDesc = object.Description
next
Set objWMISvce = Nothing
Set WshNetwork = WScript.CreateObject("WScript.Network")
strDomain = WshNetwork.UserDomain
strComputer = WshNetwork.ComputerName
strUser = WshNetwork.Username
strText = strUser & " " & strComputer & " " & strDomain
strFile = "\" & strUser & "_time.txt"
strLogin = "Logon"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
End If
If objFSO.FileExists(strDirectory & strFile) Then
Set objFolder = objFSO.GetFolder(strDirectory)
Else
Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
End If
set objFile = objFSO.GetFile(strDirectory & strFile)
strDate = objFile.DateLastModified
strText = strText & " " & strLogin & " " & strDate & " " & strIP & " " & strMac & " " & strDesc
objFile.Delete
set objFile = Nothing
set objFolder = Nothing
Const ForAppending = 8
strDirectory = "C:\user_logs$"
strFile = "\" & strUser & ".txt"
Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)
objTextFile.WriteLine(strText)
objTextFile.Close
Wscript.Quit
When I log on to my computer and the logon script runs for the first time, it creates a file with my username and the txt extention in the c:\user_logs$ directory, or mike.txt. Each time I log on thereafter, the script runs and continues to append the file with new entries. The file looks like this:
Mike FileBoss FileBoss Logon 3/6/2009 8:55:16 PM 192.168.1.250 00:E0:18:B0:71:02 Office
Mike FileBoss FileBoss Logon 3/6/2009 8:58:17 PM 192.168.1.250 00:E0:18:B0:71:02 Office
Mike FileBoss FileBoss Logon 3/6/2009 9:00:32 PM 192.168.1.250 00:E0:18:B0:71:02 Office
Mike FileBoss FileBoss Logon 3/6/2009 9:00:55 PM 192.168.1.250 00:E0:18:B0:71:02 Office
Mike FileBoss FileBoss Logon 3/6/2009 9:01:15 PM 192.168.1.250 00:E0:18:B0:71:02 Office
Mike FileBoss FileBoss Logon 3/6/2009 9:01:18 PM 192.168.1.250 00:E0:18:B0:71:02 Office
remember, that since my computer is not on a domain, the computer name shows up twice, once as the domain name, and once as the computer name. That's why you see the “FileBoss” entry twice. On a domain, you would see the domain name, and the computer name, or hostname. Each line contains an entry for username, domain name, computer name, logon(or logoff), the date, time, am or pm, my ip address, my mac address, and the description field from my network settings. To make this information more useful, it needs to be displayed graphically.
I've written a php web page to parse the user text files, which is only one on my own computer, and display the user information you want to view in a list box which looks like this:

When the user is selected from the list, and the submit button is pressed, the text file for this user is read and the data is displayed in a table that looks like this:
