Hi Guys
I have this script I'm working on that will go through the security logs of our domain controllers and return event id 672 category account logon. Basically I need to filter the events so that the event is = 672, the user ID begins with an underscore _gmagerr for example and the pre-authentication type = 2. Here's what I have. It just gets event id 672. Another thing that would be cool is if it would only return new events. Thanks in advance.
I have this script I'm working on that will go through the security logs of our domain controllers and return event id 672 category account logon. Basically I need to filter the events so that the event is = 672, the user ID begins with an underscore _gmagerr for example and the pre-authentication type = 2. Here's what I have. It just gets event id 672. Another thing that would be cool is if it would only return new events. Thanks in advance.
Code:
'==========================================================================
'
' NAME: Get EventID 672
'
' AUTHOR: Guy Thomas Modified By Gene Magerr
' EMAIL: genemagerr@hotmail.com
'
' COMMENT:
'
' VERSION HISTORY:
' 1.0 09/21/07 Initial release
'
'==========================================================================
' VBScript Prefix Naming Standard
'==========================================================================
' arr Array Contains an array of variables.
' bln Boolean Can contain either True or False
' byt Byte Integer value in the range of 0 to 255.
' col Collection Technically, a collection is not a variable subtype.
' It is mentioned in this table because it is good
' practice to use the col prefix to indicate a collection.
' Collections are used extensively in system administration
' scripts.
' (None)Constant Constant values contain no prefix and use UPPER Case
' letters with underscores. Constants cannot be altered
' like normal variables.
' cur Currency Range of -922,337,203,685,477.5808 to
' 922,337,203,685,477.5807
' dbl Double Contains a double-precision floating-point number in
' the range
' dic Dictionary Scripting dictionaries
' dtm Date (Time) either a Date, Time, or Date and Time
' err Error Contains an error number value.
' fun Functions Programatic Function
' g_ Globals Variables with global scope
' int Integer Contains integer value in the range of -32,768 to 32,767.
' lng Long Contains an integer value in the range -2,147,483,648 To
' 2,147,483,647.
' obj Object Contains a reference to an Object.
' sng Single Contains a single-precision floating-point number
' str String A variable length string of textual data
' sub Subroutines Programatting SubRouting
' var Variant A variable that can store different data types at
' different times.
'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Option Explicit
Dim objFso, objFolder, objWMI, objEvent, objTextFile, dtmEventDate ' Objects
Dim strFile, strComputer, strFolder, strFileName, strPath, strTimeWritten ' Strings
Dim intEvent, intNumberID, intRecordNum, colLoggedEvents, objEmail
'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
intNumberID = 672 ' Event ID Number
intEvent = 1
intRecordNum = 1
strFileName = "\Event672.txt"
strFolder = "C:\GeneTest"
strPath = strFolder & strFileName
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolder) Then
Set objFolder = objFSO.GetFolder(strFolder)
Else
Set objFolder = objFSO.CreateFolder(strFolder)
Wscript.Echo "Folder created " & strFolder
End If
Set strFile = objFso.CreateTextFile(strPath, True)
Const ForReading = 1
Set objTextFile = objFSO.OpenTextFile("C:\servers.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline
Set objWMI = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colLoggedEvents = objWMI.ExecQuery ("Select * from Win32_NTLogEvent Where Logfile = 'Security'" )
'Wscript.Echo " Press OK and Wait 30 seconds (ish)"
intEvent = 1
For Each objEvent in colLoggedEvents
If objEvent.EventCode = intNumberID Then
strFile.WriteLine ("Record No: ")& intEvent
strFile.WriteLine ("Category: " & objEvent.Category)
strFile.WriteLine ("Computer Name: " & objEvent.ComputerName)
strFile.WriteLine ("Event Code: " & objEvent.EventCode)
strFile.WriteLine ("Message: " & objEvent.Message)
strFile.WriteLine ("Record Number: " & objEvent.RecordNumber)
strFile.WriteLine ("Source Name: " & objEvent.SourceName)
'strFile.WriteLine ("Time Written: " & objEvent.TimeWritten)
dtmEventDate = objEvent.TimeWritten
strTimeWritten = WMIDateStringToDate(dtmEventDate)
strFile.WriteLine "Time Written: " & strTimeWritten
strFile.WriteLine ("Event Type: " & objEvent.Type)
strFile.WriteLine ("User: " & objEvent.User)
strFile.WriteLine (" ")
strFile.WriteLine ("===========================================================================")
strFile.WriteLine (" ")
intRecordNum = intRecordNum +1
End if
IntEvent = intEvent +1
Next
Wscript.Echo "Check " & strPath & " for " &intRecordNum & " events"
Loop
objTextFile.Close
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "gmagerr@hotmail.com"
objEmail.To = "gmagerr@hotmail.com"
objEmail.Subject = strComputer & " EventID 672"
intEvent = 1
For Each objEvent in colLoggedEvents
If objEvent.EventCode = intNumberID Then
objEmail.TextBody = objEmail.TextBody & ("Record No: ")& intEvent & vbCrLf
objEmail.TextBody = objEmail.TextBody & ("Category: " & objEvent.Category) & vbCrLf
objEmail.TextBody = objEmail.TextBody & ("Computer Name: " & objEvent.ComputerName) & vbCrLf
objEmail.TextBody = objEmail.TextBody & ("Event Code: " & objEvent.EventCode) & vbCrLf
objEmail.TextBody = objEmail.TextBody & ("Message: " & objEvent.Message) & vbCrLf
objEmail.TextBody = objEmail.TextBody & ("Record Number: " & objEvent.RecordNumber) & vbCrLf
objEmail.TextBody = objEmail.TextBody & ("Source Name: " & objEvent.SourceName) & vbCrLf
'objEmail.TextBody = objEmail.TextBody & ("Time Written: " & objEvent.TimeWritten) & vbCrLf
dtmEventDate = objEvent.TimeWritten
strTimeWritten = WMIDateStringToDate(dtmEventDate)
objEmail.TextBody = objEmail.TextBody & ("Time Written: " & strTimeWritten) & vbCrLf
objEmail.TextBody = objEmail.TextBody & ("Event Type: " & objEvent.Type) & vbCrLf
objEmail.TextBody = objEmail.TextBody & ("User: " & objEvent.User) & vbCrLf
objEmail.TextBody = objEmail.TextBody & (" ") & vbCrLf
objEmail.TextBody = objEmail.TextBody & ("===========================================================================") & vbCrLf
objEmail.TextBody = objEmail.TextBody & (" ") & vbCrLf
intRecordNum = intRecordNum +1
End if
IntEvent = intEvent +1
Next
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = "smtp.server"
objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
objEmail.Configuration.Fields.Update
objEmail.Send
WScript.Quit
'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================
'==========================================================================
' This function will take the objEvent.TimeWritten which returns the time
' using WMI’s default Universal Time Coordinate format. In other words,
' you’ll get back results similar to this: 20041025124000.000000-420
' and convert it to the proper date and time format
'==========================================================================
Function WMIDateStringToDate(dtmEventDate)
WMIDateStringToDate = CDate(Mid(dtmEventDate, 5, 2) & "/" & _
Mid(dtmEventDate, 7, 2) & "/" & Left(dtmEventDate, 4) _
& " " & Mid (dtmEventDate, 9, 2) & ":" & _
Mid(dtmEventDate, 11, 2) & ":" & Mid(dtmEventDate, 13, 2))
End Function