Query Security Log with VBScript

I'd like to get a vbscript to query the Windows security log at regular intervals and notify me of the number of failed log in attempts in the past 10-30 minutes.

I have a VBScript script queries through WMI that works on a basic level, but it is extremely slow (60+ seconds). (you can see the query there below). The slowness problem maybe that it grabs all of the events queried, but try as I might, I cannot find/figure out the correct sytnax for querying just the time I want. (see second code snippit for one of my attempts) I've tried all sorts of diferent date formats. Is it even possible to query via date?

Is there a secondary approach I can take to getting the same data with a quicker method? i.e. using something other than WMI?



"Select * From Win32_NTLogEvent Where EventType <> 0 AND EventType <> 2 AND EventType <> 3 AND EventType <> 4 AND EventIdentifier = 529"
 
"Select * From Win32_NTLogEvent Where EventType <> 0 AND EventType <> 2 AND EventType <> 3 AND EventType <> 4 AND EventIdentifier = 529 AND TimeGenerated > 20090525155200.000000-000 "

Open in new window

LVL 1
ITtelligentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi there. Try using the TimeWritten property, and see the code below, which retrieves the TimeBias, and formats todays date to the format required for TimeWritten.

Change the plus or minus in the TimeBias to suit your time zone.

This will currently return all events in the security log since midnight.

Regards,

Rob.
strComputer = "."
strLogName = "Security"
 
strTimeBias = Get_CurrentTimeZone_Of_Computer(strComputer)
' Change this to plus or minus, depending on your time zone
strTimeBias = "+" & strTimeBias
 
strDateFrom = Year(Now) & Right("0" & Month(Now), 2) & Right("0" & Day(Now), 2) & "000000.000000" & strTimeBias
 
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colLoggedEvents = objWMI.ExecQuery _
						("SELECT * FROM Win32_NTLogEvent WHERE LogFile = '" & strLogName & _
							"' AND EventType = 1 AND EventIdentifier = 529 AND TimeWritten >= '" & _
							strDateFrom & "'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
 
For Each objEvent in colLoggedEvents
	MsgBox objEvent.Message
Next
 
MsgBox "Done"
 
Function Get_CurrentTimeZone_Of_Computer(byval strComputerName)
 
	Dim objWMIService, colLogFiles, objLogFile, intTotal, colItems, objItem, strCurrentTimeZone
	Const wbemFlagReturnImmediately = &h10
	Const wbemFlagForwardOnly = &h20
 
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
		strComputerName & "\root\cimv2")			
 
	Set colItems = objWMIService.ExecQuery("Select CurrentTimeZone from Win32_OperatingSystem",,48)
 
	On Error Resume Next
	For Each objItem in colItems
		If Err.Number = 0 Then
			On Error GoTo 0
			strCurrentTimeZone = objItem.CurrentTimeZone
			Exit For
		Else
			MsgBox "Unknown Error during Time Bias for " & strComputer & "." & vbCrLf & "Error Number: " & _
				Err.Number & vbCrLf & "Error Description: " & Err.Description, vbOKOnly, "Unknown Error"
			Err.Clear
			On Error GoTo 0
			Exit For
		End If
	Next
	On Error GoTo 0
 
	Get_CurrentTimeZone_Of_Computer = strCurrentTimeZone
	
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITtelligentAuthor Commented:
Can you shed some light on the SearchTimeBias? Is it that the events stored in GMT and we have to add the correct modifier to achive a valid time?

0
RobSampsonCommented:
The TimeWritten property (and other time related properties) are stored using WMI's default Universal Time Coordinate (UTC) format, as shown here:

Can I Retrieve Just Failure Events from the Security Event Log?
http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct04/hey1026.mspx

Why it's like that I'm not sure, but on that page is also a function to format that time back to normal time.

So because the property is stored in UTC, you need to convert the time frame you're looking for into that same format so that it can make the comparison.

Regards,

Rob.
0
ITtelligentAuthor Commented:
I googled for an hour looking how to get the format correct - that link would have been perfect!

Your above code works great, with the excpetion the EventType turns out to be 5 in my case, not 1.

Thanks for the help!
0
RobSampsonCommented:
Ha ha, no problem! Thanks for the grade.  I only knew where to look for that article because I knew the Scripting Guys have done a bit of Event Log code....check the archives there too for some more samples.

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.