Vbscript Win32\u NTLogEvent消息属性FilSystemObject.写入过程调用问题
我正在编写一个脚本,将事件日志信息写入csv。我的脚本以前是有效的。但是我改变了一些东西,这不应该对写csv有任何影响。基于我在互联网上对这个问题所做的一些研究,我已经试着用unicode和ASCII编码。两者都没有区别。奇怪的是,我稍后在脚本中使用相同的代码来编写其他日志(我首先编写系统日志,然后编写应用程序日志,等等),它在那里工作得非常好。我使用的代码是临时性的,因为我还没有找到从消息中删除回车的方法(这会导致将CSV导入Excel时出现问题)。所以一旦我这么做,它可能会自行修复。但这似乎是一个更大的问题。以下是脚本,直到它移动到其他日志:Vbscript Win32\u NTLogEvent消息属性FilSystemObject.写入过程调用问题,vbscript,event-log,filesystemobject,Vbscript,Event Log,Filesystemobject,我正在编写一个脚本,将事件日志信息写入csv。我的脚本以前是有效的。但是我改变了一些东西,这不应该对写csv有任何影响。基于我在互联网上对这个问题所做的一些研究,我已经试着用unicode和ASCII编码。两者都没有区别。奇怪的是,我稍后在脚本中使用相同的代码来编写其他日志(我首先编写系统日志,然后编写应用程序日志,等等),它在那里工作得非常好。我使用的代码是临时性的,因为我还没有找到从消息中删除回车的方法(这会导致将CSV导入Excel时出现问题)。所以一旦我这么做,它可能会自行修复。但这似乎
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
strComputer = "."
strType = "Error"
strPath = "T:\IT resources\Event Logs\ErrorLog" & strComputerName & ".csv"
'Script to convert UTC to human readable. From Script Repository.
Function WMIDateStringToDate(dtmInstallDate)
WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _
& " " & Mid (dtmInstallDate, 9, 2) & ":" & _
Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, _
13, 2))
End Function
'ForWriting is to write to file from start. ForAppending is to write to file from end of file.
constForWriting = 2
constForAppending = 8
constTristate = 0
boolUnicode = False
chrCarriageReturn = chr(13)
chrNewLine = chr(10)
Set objFSO = CreateObject("Scripting.FileSystemObject")
'This is so that cscript won't encounter a runtime error if the file already exists. Also so that it will write to the already existing file.
If objFSO.FileExists(strPath)=False Then
Set objErrLog = objFSO.CreateTextFile(strPath,constForWriting,boolUnicode)
objErrLog.Write "Type,"
objErrLog.Write "Time Generated,"
objErrLog.Write "Source Name,"
objErrLog.Write "Event Code,"
objErrLog.Write "Category,"
objErrLog.Write "Message"
objErrLog.Writeline
strTimeMin = "01/01/1970/0:00:00"
'19700101000000.000000-480
Else Set objErrLog = objFSO.OpenTextFile(strPath,constForAppending,constTristate)
'Only need this if it writes from the line the file ends on, as opposed to starting on a new line (which I expect it will).
objErrLog.WriteLine
End If
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'Querying Event Logs
Set colLoggedEvents = objWMIService.ExecQuery _
("SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'system' AND "_
& "Type = 'Error'")
'Type='Error' instead of "1" because it is a WQL query, I think. I believe that it is searching the entries in a database that reference the Win32_NTLogEvent objects. So I am searching the values in the database as opposed to the properties of the objects they reference. Or perhaps not. WHen I echo the type property of every object in colLoggedEvents, cscript outputs "Error". So maybe the I'm reading the SDK wrong? At least it seems to be working.
'This is a comparison function which tells where string 2 occurs in string 1. Starts at 1.
constStart = 1
constCompareType = 0
'This loop writes the information to a .csv.
For Each objEvent In colLoggedEvents
If objEvent.Timegenerated > strTimeMin Then
strTimeMin = objEvent.TimeGenerated
Else
End If
objErrLog.Write objEvent.Type & ","
objErrLog.Write WMIDateStringToDate(objEvent.TimeGenerated) & ","
objErrLog.Write objEvent.SourceName & ","
objErrLog.Write objEvent.EventCode & ","
constExist=InStr(constStart,objEvent.Message,chrCarriageReturn,constCompareType)+InStr(constStart,objEvent.Message,chrNewLine,constCompareType)
If constExist = 0 Then
objErrLog.Write objEvent.Category & ","
objErrLog.Write objEvent.Message
Else
objErrLog.Write objEvent.Category
End If
objErrLog.WriteLine
Next
任何帮助都将不胜感激
object.CreateTextFile(filename:string[, overwrite:bool[, unicode:bool]])
object.OpenTextFile(filename:string[, iomode:enum[, create:bool[, format:enum]]])
这与你的想法相冲突
objFSO.CreateTextFile(strPath,constForWriting,boolUnicode)
objFSO.OpenTextFile(strPath,constForAppending,constTristate)
.OpenTextFile的原型是
object.CreateTextFile(filename:string[, overwrite:bool[, unicode:bool]])
object.OpenTextFile(filename:string[, iomode:enum[, create:bool[, format:enum]]])
这与你的想法相冲突
objFSO.CreateTextFile(strPath,constForWriting,boolUnicode)
objFSO.OpenTextFile(strPath,constForAppending,constTristate)
所以,请(自己)修复这些错误,使用真正为Unicode打开的文件进行测试,并希望假设(3)成立
更新wrt评论:
请思考“提出问题时给出完整的错误详细信息(编号、说明、识别的行)”,内容如下:
在colLoggedEvents的68个成员之后,我得到一个无效的过程错误
当我有ASCII格式的文件时
vs
调用OpenTextFile方法时出现错误
第一条语句表示第68个成员包含无法在ASCII/ANSI模式下写入的字符如果错误日志不包含无效数据,则使用Unicode输出格式将修复此问题
第二条语句表明.Open/CreateTextfile方法的参数仍然不正确。您是否将两个调用都更改为Unicode
更新II:
文档定义了
TristateTrue-1以Unicode格式打开文件
您的代码错误地使用了:
constTristate = 1
Set objErrLog = objFSO.OpenTextFile(strPath,constForAppending,boolCreate,constTristate)
证据:
>> Set ts = goFS.OpenTextFile("error5.txt", 8, False, 1)
>>
Error Number: 5
Error Description: Invalid procedure call or argument
>> Set ts = goFS.OpenTextFile("error5.txt", 8, False, -1)
>>
>> <-- no news are good news
Adam文件涉及VBA问题;但是如果没有检查,我不会相信它。是的,我注意到我忘了包括创建选项。我现在已经修好了。昨天我很着急,因为这让我很沮丧。所以我错失了这个选择。解决该问题后,更改为unicode会使问题更快出现。当我有ASCII格式的文件时,在colLoggedEvents的68个成员之后,我得到一个无效的过程错误。但在Unicode中,它无法通过任何。此外,我并不是说问题会自行解决。我的意思是,按照我说的去做可以解决这个问题。因为我无论如何都会更改相关的代码部分。我的意思是说我在调用OpenTextFile方法时出错。但我无意中按了回车键。所以我的回复贴出来了,我花了太长时间编辑它。我已经检查了OpenTextFile的所有选项,看起来现在应该都可以工作了。我有:“OpenTextFile constforAppend=8 boolCreate=False constTristate=1 Set objErrLog=objFSO的选项。OpenTextFile(strPath、constforAppend、boolCreate、constTristate)我肯定都改成了unicode。createtextfile方法读取Set-objErrLog=objFSO.createtextfile(strPath、boolOverwrite、boolUnicode),boolOverwrite=False,boolUnicode=True。OpenTextFile方法读取Set-objErrLog=objFSO.OpenTextFile(strPath,constforappend,boolCreate,constTristate),constforappend=8,boolCreate=False,constTristate=1。我已经一次又一次地检查它,确保我有正确的价值观。但它在OpenTextFile调用时出错。CreateTextFile调用没有问题。@AdamHarvey-请参阅更新II。好的,更改三态上的符号可以修复它。谢谢你的帮助。然而,真正奇怪的是,MSDN上显示的是正1。我刚才仔细检查了一遍,以确保我没有不知何故把它误读了20遍。我正在看这页()。您使用的是什么文档?