nnsense
11/20/2013 - 4:47 PM

WMI - Fetch eventlog tra due date

WMI - Fetch eventlog tra due date

strComputer = "."
dtmStartDateTime = Date2WMIdate(DateAdd("h","-2",Now))
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colLogEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent WHERE LogFile = 'System' AND (Type = 'Warning' OR Type = 'Error') AND TimeWritten >= '" & dtmStartDateTime & "'")

For Each objItem in colLogEvents
  wscript.echo objItem.message
Next

Function Date2WMIdate(strDate)
	arrDate = Split(strDate," ")
	strDate = arrDate(0)
	strTime = arrDate(1)
	
	dtmYear = Year(strDate)
	dtmMonth = Month(strDate)
	dtmDay = Day(strDate)
	
	dtmHour = Hour(strTime)
	dtmMin = Minute(strTime)
	dtmSec = Second(strTime)
	
	dtmMonth = addZero(dtmMonth)
	dtmDay = addZero(dtmDay)
	dtmHour = addZero(dtmHour)
	dtmMin = addZero(dtmMin)
	dtmSec = addZero(dtmSec)
	
	Date2WMIdate = dtmYear & dtmMonth & dtmDay & dtmHour & dtmMin & dtmSec & ".000000-000"
End Function

Function addZero(intPartofDateTime)
	if Len(intPartofDateTime) = 1 Then: addZero = "0" & intPartofDateTime: Else: addZero = intPartofDateTime: End If
End Function