Page 1 of 1 1
Topic Options
#124650 - 2004-08-09 01:51 PM WMI Winmgmts searching Win32_NTLogEvent by date and time
Scrib Offline
Fresh Scripter

Registered: 2002-07-08
Posts: 13
Loc: uk
Why doesnt this work?

If I do a query on the NT eventlog using WMI on the date only it works fine. If I then want to reduce the search results by adding in the time to the search query is doesn't work.

Note: I've tried it without subtracting 1 from $the_hour and just changing the system time to one hour ago and it still doesnt work.

I am wondering if its down to the varibles only holding a limited number size?

Code as follows:

Break on

Generate_simple_date_varibles ()

$the_hour = Val($the_hour)
If $the_hour > 1
$the_hour = $the_hour - 1
EndIf


$Eventvwr_Date = $the_year + $the_month + $the_day + $the_hour + $the_minute + $the_second
;$Eventvwr_Date = $the_year + $the_month + $the_day ; This works with no time - why?

?"Search date time: $Eventvwr_Date"?

$EventLog_count_All = 0
$Eventvwr_log = Application
$Eventvwr_Type = information

$events = GetObject("Winmgmts:\\@WKSTA").ExecQuery("Select * from Win32_NTLogEvent Where Type = '$Eventvwr_Type' AND Logfile = '$Eventvwr_log' AND TimeWritten >= '$Eventvwr_Date' ")
For Each $event In $events

?"Orig EVT date time: " + $event.TimeWritten
$EventLog_count_All = $EventLog_count_All + 1

Next

??"Count is $EventLog_count_All"

?"press any key"
Get $dummy
Exit


Function Generate_simple_date_varibles ()

$the_date_unstripped = @DATE
$the_time_unstripped = @TIME
$the_year = SubStr($the_date_unstripped,1,4)
$the_month = SubStr($the_date_unstripped,6,2)
$the_day = SubStr($the_date_unstripped,9,2)
$the_hour = SubStr($the_time_unstripped,1,2)
$the_minute = SubStr($the_time_unstripped,4,2)
$the_second = SubStr($the_time_unstripped,7,2)

EndFunction

Top
#124651 - 2004-08-09 02:35 PM Re: WMI Winmgmts searching Win32_NTLogEvent by date and time
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Why aren't you using DATECALC()/SERIALDATE() UDF(s)?

They will make your life easier.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#124652 - 2004-08-10 01:26 PM Re: WMI Winmgmts searching Win32_NTLogEvent by date and time
Scrib Offline
Fresh Scripter

Registered: 2002-07-08
Posts: 13
Loc: uk
Thanks for your help.

I've had a look the the UDFs you mentioned and others, but I don't understand how these will help. The format for this WMI query must be yyyymmddhhmmss with no colons.

It works fine running the query using yyyymmdd but fails when adding the time.

I've now tried the same thing using CDbl, stings, etc with no avail.

Any ideas?

Top
#124653 - 2004-08-10 02:09 PM Re: WMI Winmgmts searching Win32_NTLogEvent by date and time
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You have to include the entire timestamp, including the milliseconds and the offset from GMT (UTC).

Here is an example.

Note, I have hard-coded the offset as "+0", and simply subtracting "1" from the hour is a bit less than smart, but I'll leave you to sort those out

Code:
$=SetOption("Explicit","ON")
$=SetOption("WrapAtEOL","ON")
$=SetOption("ASCII","ON")

Dim $sEvtLog,$sEvtType,$sEvtTimestamp
Dim $oEvents,$oEvent
Dim $asNow

$sEvtLog = "System"
$sEvtType = "Information"

$asNow=Split(@TIME,":")
$sEvtTimestamp=CStr(Join(Split(@DATE,"/"),""))
+CStr(Right("0"+(Cint($asNow[0])-1),2))
+$asNow[1]
+$asNow[2]
+".000000"
+"+0" ; OFFSET FROM GMT (in minutes)

$sEvtTimeStamp ?

$oEvents = GetObject("Winmgmts:\\@WKSTA").ExecQuery("
Select * from Win32_NTLogEvent
Where
Type = '"+$sEvtType+"'
AND Logfile = '"+$sEvtLog+"'
AND TimeWritten >= '"+$sEvtTimestamp+"'
")

For Each $oEvent In $oEvents
"Orig EVT date time: " + $oEvent.TimeWritten ?
Next

Exit 0


Top
#124654 - 2004-08-11 03:58 AM Re: WMI Winmgmts searching Win32_NTLogEvent by date and time
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
See also ReadEventlog() , which does explain and demonstrate how to query an eventlog based on dates/times.
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1471 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.057 seconds in which 0.03 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org