VBScript Logging

Hello,

I have a log that I use during logon that captures certain information (time/date, computer name, user name, ip and mac address etc. and has been working well for me to date. However, I have had need on occasion to use these routines for other tasks I was assigned to do. I need to be able to (of course) log each login event, however, I would like it to search for a previous entry (via computer name) and instead of creating a new entry, to modify the previous entry with new logon information, and I am not sure how to accomplish this. Can you provide me with a way to update an existing entry based upon the computer name?

Here is some of the code I'm using at the moment, its pretty basic but gets the job done.

SubLogIt()

Path="\\Server\share\Logon Info.csv"
ConstForReading=1
ConstForWriting=2
ConstForAppending=8

Iffso.FileExists(Path)=0Then
'LogFilenotfound-CreateLogFile
SetoFile=fso.CreateTextFile(Path,True)
oFile.WriteLine("Date&Time"&","&"ComputerName"&","&"NTUserID"&","&"FirstName"&","&"LastName"&","&"OperatingSystem"&","&"Version"&","&"IP Address"&","&"Mac Address"&vbCrLf)
oFile.Close
WScript.Sleep(500)
EndIf
SetoFile=fso.OpenTextFile(Path,ForAppending)
oFile.Write(Now()&","&strComputer&","&strUserName&","&strGivenName&","&oUser.sn&","&strCaption&","&strArch&","&strIP&","&strMac&vbCrLf)
oFile.Close


EndSub

Any help would be appreciated.

August 31st, 2015 12:19pm

You are asking for someone to design a solution for you.  We can answer specific question.

TO manipulate and edit files you need to have a structure and a method of access.  An INI file or XML file can be selectlvely edited.  A text file has to be recreated on each edit making it a very poor choice.

Why do you think you need to do this?

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 12:27pm

I'm not necessarily asking for a designed solution, I'm not terrible with VBScript, but I'm no Scripting guy either :) I'm looking for a way to replace with new login information, something already in a text file (CSV actually). For example, if Computer123 logs in at 8am with a username john1, ip address 192.168.0.2 and mac address <pick something>.... then three hours later, Computer 123 logs in again, I want to update the older information in the same slot the last computer123 had with the new logon information, instead of creating a new entry in the log for the same computer. This routine would also work well where in install during logon would initially report an attempted install and the next time the machine logged in, the report could show a successful or not successful install

I was thinking of perhaps reading the entire csv file into a dictionary perhaps, then looking for the computer123, then overwriting computer123 info back into the csv file again, but I don't know how I would associate the other information that computer123 originally came in with and overwrite it with the new information from the new logon. That's what i'm asking :)

August 31st, 2015 3:42pm

I was thinking of perhaps reading the entire csv file into a dictionary perhaps, then looking for the computer123, then overwriting computer123 info back into the csv file again, but I don't know how I would associate the other information that computer123 originally came in with and overwrite it with the new information from the new logon. That's what i'm asking :)

If I had limited VB Scripting skills then I would start like so:

  • Write the pseudo code.
  • Learn how to use the tools required by the pseudo-code, e.g.
    - How to read a text file into an array of lines.
    - How to search this array for a computer name.
    - How to replace an existing array element with a new element.
    - How to write the array back into a text file.

This approach would get you 80 or 90% towards your goal. It would also let you ask specific questions in this forum rather than asking for a turn-key solution.

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 4:15pm

You can use ADO to edit a CSV file directly.  Just load the CSV as a recordset and edit it.  If you need to you can use UPDATE/DELETE/INSERT statements.  Search for examples.

If I were doing it I would use an XML file because it is much easier to edit in VBScript.

August 31st, 2015 6:30pm

Example:

UPDATE 'c:\test.csv' Set LogonTime='12:30' WHERE Computer = 'JoesPC';

That is all it takes with ADO.

For XML

Set n = xml.SelectSingleNode("//computer[@name='JoesPC']")
n.LogonTime ="12:30"
xml.Save(strXmlFileName)

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 6:34pm

I was thinking of perhaps reading the entire csv file into a dictionary perhaps, then looking for the computer123, then overwriting computer123 info back into the csv file again, but I don't know how I would associate the other information that computer123 originally came in with and overwrite it with the new information from the new logon. That's what i'm asking :)

If I had limited VB Scripting skills then I would start like so:

  • Write the pseudo code.
  • Learn how to use the tools required by the pseudo-code, e.g.
    - How to read a text file into an array of lines.
    - How to search this array for a computer name.
    - How to replace an existing array element with a new element.
    - How to write the array back into a text file.

This approach would get you 80 or 90% towards your goal. It would also let you ask specific questions in this forum rather than asking for a turn-key solution.

August 31st, 2015 8:12pm

thank you.
Free Windows Admin Tool Kit Click here and download it now
September 1st, 2015 12:58pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics