2

Below is my VBS code. I will admit, I am extremely new to this since I was give this as a job at work. I need to create a logon/logoff script that will capture certain information and store it in a csv file. I am able to get the information and store it in the csv file, but when I try to do it again, I want it to create another row and store the updated information there. I keep getting these asian characters. What seems to be the problem.

This is what I get on the second time I click my log off script: ਍潙牵琠硥⁴潧獥栠牥⹥਍

' ******************** Log Off Script **********************************
'Script to write Logoff Data Username, Computername to eventlog.

Dim objShell, WshNetwork, PCName, UserName, strMessage, strContents, logDate, logTime
Dim strQuery, objWMIService, colItems, strIP, rowCount

' Constants for type of event log entry
const EVENTLOG_AUDIT_SUCCESS = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")

logDate = Date()
logTime = Time()
PCName = WshNetwork.ComputerName 
UserName = WshNetwork.UserName  
strMessage = "Logoff Event Data" & "PC Name: " & PCName & "Username: " & UserName & "Date: " & logDate & "Time: " & logTime

If (objFSO.FileExists("test.csv")) Then
WScript.Echo("File exists!")
dim filesys, filetxt 
Const ForReading = 1, ForWriting = 2, ForAppending = 8 
Set filesys = CreateObject("Scripting.FileSystemObject") 
Set filetxt = filesys.OpenTextFile("test.csv", ForAppending, True) 
filetxt.WriteLine(vbNewline & "Your text goes here.") 
filetxt.Close 

Else
rowCount = rowCount + 1
WScript.Echo("File does not exist! File Created!")
Set csvFile = objFSO.CreateTextFile("test.csv", _ 
ForWriting, True)

objShell.LogEvent EVENTLOG_AUDIT_SUCCESS, strMessage
csvFile.Write strMessage
csvFile.Writeline

End If  

WScript.Quit

1 Answer 1

5

When in doubt, read the documentation. You're creating the file in Unicode format (3rd argument of CreateTextFile set to True):

Set csvFile = objFSO.CreateTextFile("test.csv", _
ForWriting, True)

but you open an existing (Unicode) file in ASCII format (4th argument of OpenTextFile not specified):

Set filetxt = filesys.OpenTextFile("test.csv", ForAppending, True) 

Either open a file in Unicode format all of the time, or never.

Also, it's unnecessary to create more than one FileSystemObject instance. Just use the one you created at the beginning throughout the entire script. You can also use the same variable for the text stream object.

If you want to use Unicode format you need to change the above two lines into this:

Set csvFile = objFSO.CreateTextFile("test.csv", False, True)
...
Set csvFile = objFSO.OpenTextFile("test.csv", ForAppending, False, True)

otherwise into this:

Set csvFile = objFSO.CreateTextFile("test.csv")
...
Set csvFile = objFSO.OpenTextFile("test.csv", ForAppending)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.