开发者

use wmi to get newest Windows log events

开发者 https://www.devze.com 2022-12-18 01:42 出处:网络
I want to use WMI to monitor the Windows event log and开发者_StackOverflow中文版 get newest log events every 15 minute. Although I can use WQL to do the query, it does not have the keywords such as or

I want to use WMI to monitor the Windows event log and开发者_StackOverflow中文版 get newest log events every 15 minute. Although I can use WQL to do the query, it does not have the keywords such as order by. Any ideas how to work around this problem?


you can use a dataset. Below is done using vbscript, and only on the fields ComputerName,EventCode and Message. Add the other fields as desired

Const adVarChar = 200
Const MaxCharacters = 1024
Const adFldIsNullable = 32
Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "ComputerName", adVarChar, MaxCharacters,adFldIsNullable
DataList.Fields.Append "EventCode", adVarChar, MaxCharacters,adFldIsNullable
DataList.Fields.Append "Message",adVarChar,MaxCharacters,adFldIsNullable
DataList.Open
strComputer = "."
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colLoggedEvents = objWMIService.ExecQuery("Select * from Win32_NTLogEvent Where Logfile = 'Application'")
For Each evt in colLoggedEvents
 DataList.AddNew
 DataList("ComputerName") = evt.ComputerName
 DataList("EventCode") = evt.EventCode
 DataList("Message") = evt.Message
 DataList.Update
Next
'sort by eventcode
DataList..Sort = "EventCode DESC"
DataList.MoveFirst
Do Until DataList.EOF
 Wscript.Echo DataList.Fields.Item("ComputerName") & vbTab & DataList.Fields.Item("EventCode") & vbTab & DataList.Fields.Item("Message")
DataList.MoveNext
Loop
0

精彩评论

暂无评论...
验证码 换一张
取 消