SWbemDateTime to Win 2k Compatible

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



ok, i posted this here earlier and found what i was looking for. but when i
went to run on the server i found out that it didn't work. so try as i might
to make it work it could not get it. so here i am again asking for your help
with my problems.

i got the script to work in win xp but not in 2k, all that i need fixed is
to get two UTC dates that are hours apart not days. the comments should help
a little. if the formating look off thats because i used notepad++ and it
formats differently then notepad. but it looks real nice in there. if you
have questions about it please email me: pat at mssinc dot com

thanks again for all your help.

Script in question:

'***************PTR**************
' Check Backup Exec Script
' 12/5/2007

'=========================
'Prepare text files for query
'=========================

Const ForReading = 1
Const ForWriting = 2

' ptr this next line assigns the new text file name and location
strFileName1 = "c:\ktemp\Backup\" & "AppLog.csv"
strFileName2 = "c:\ktemp\Backup\" & "BackupEvent.txt"

' ptr these next lines create the text files
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(strFileName1)
Set objFile1 = objFSO.CreateTextFile(strFileName2)

'==================
'Query the Event log
'==================
'Set constants for query
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strComputer = "."
'array for multiple computers
'set WMI service, query all logs, and set namespace
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate,(Security)}!\\" & _
strComputer & "\root\CIMV2")

'-------From the internet and msdn forums-----------
'restrict query to a period between now and 2 days ago

'create date conversion variables and functions
starthour = DateAdd("h",-10,Now)

dtmEndDate = CDate(Date)
dtmStartDate = dtmEndDate - 1


' Converting to WMI "date"
dtmEndDate = Year(dtmEndDate) _
& Right( "00" & Month(dtmEndDate), 2) _
& Right( "00" & Day(dtmEndDate), 2)

dtmStartDate = Year(dtmStartDate) _
& Right( "00" & Month(dtmStartDate), 2) _
& Right( "00" & Day(dtmStartDate), 2)

'------------------NOT Win 2k Complient--------------------------------------
'Set dtmStart = CreateObject("WbemScripting.SWbemDateTime")
'dtmStart.SetVarDate starthour
'WScript.Echo dtmStart

'Set dtmEnd = CreateObject("WbemScripting.SWbemDateTime")
'dtmEnd.SetVarDate Now
'WScript.Echo dtmEnd

'---------------End NOT 2k Complient------------------------
'--------------------End from Forum----------------------

'get today's date for query
'Set dtmNow = CreateObject("WbemScripting.SWbemDateTime")
'dtmNow.SetVarDate Now, CONVERT_TO_LOCAL_TIME
'WScript.Echo dtmEnd

'query the event log
Set colItems = objWMIService.ExecQuery ("SELECT * FROM Win32_NTLogEvent
" & _
"WHERE Logfile = 'Application' " & _
"AND EventCode = '34112' AND SourceName = 'Backup
Exec' " & _
"AND TimeGenerated < ' " & dtmEnd & " ' " & _
"AND TimeGenerated > ' " & dtmStart & " ' " , "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

'==========================================================
'write the log entries to the text file in csv format for ADO reader.
'==========================================================

'Write header line for proper reading and searching.
objFile.Writeline("EventCode,SourceName,TimeGenerated,Message")

For Each objItem In colItems

objFile.Write(objItem.EventCode & ",")
objFile.Write(objItem.SourceName & ",")
objFile.Write(objItem.TimeGenerated & ",")
objFile.Write(objItem.Message)

Next

'------------old method of writing to text file------------------------------
'For Each objItem In colItems
' objFile.Writeline ("ComputerName: " & objItem.ComputerName)
' objFile.Writeline ("EventCode: " & objItem.EventCode)
'objFile.Writeline ("Logfile: " & objItem.Logfile)
'objFile.Writeline ("SourceName: " & objItem.SourceName)
'objFile.Writeline ("Type: " & objItem.Type)
'objFile.Writeline ("Message: " & objItem.Message)
'objFile.Writeline ("TimeGenerated: " & objItem.TimeGenerated)
' objFile.Writeline ("TimeWritten: " &
WMIDateStringToDate(objItem.TimeWritten) )
'objFile.Writeline (" ")
'Next

'-------------------end old method-------------------------------

'Date conversion function
Function WMIDateStringToDate(dtmDate)
WScript.Echo dtm:
WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" &
Mid(dtmDate,13, 2))
End Function

'close open text file before opening for reading.
objFile.Close

'=================================
'Read Applog.csv with ADO text reader
'=================================

'-----------------old Read method--------------------------
'Read the file and loop until strLine found
'Then write the line to new text file for kaseya to read.
'Do Until objFile1.AtEndOfStream

' strLine = objFile1.ReadLine

' IF LEFT(strline,17) = "EventCode:" Then

' strEvent = LEFT(strline,29)
' objFile2.Writeline (strEvent)

' End If

' IF LEFT(strline,23) = "SourceName:" Then

' strSource= LEFT(strline,29)
' objFile2.Writeline (strSource)

'End If

'IF LEFT(strline,66) = "Message:" Then

' strMessage = MID(strline,79,28)
' objFile2.Writeline (strMessage)

'End If
'Loop
'------------------------End old Method---------------------------

'----------------------------------------
'Create Varaibles and Constants
'-----------------------------------------

'Create conststants for ADO Reading
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001

'Set path to file. include trailing backslash.
strPathtoTextFile = "C:\ktemp\Backup\"

'Set connection and Reader objects
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
'-------------------------
'open connections
'-------------------------

'open OLEDB Connection
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended Properties=""text;HDR=YES;FMT=Delimited"""

'open the text file and query for text
objRecordset.Open "SELECT * FROM AppLog.csv " , _
objConnection, adOpenStatic, adLockOptimistic, adCmdText

'--------------- ADO operation from the net-------------
'Do Until objRecordset.EOF
' Wscript.Echo "Name: " & objRecordset.Fields.Item("Name")
' Wscript.Echo "Department: " & _
'
' Wscript.Echo "Extension: " & objRecordset.Fields.Item("Extension")
' objRecordset.MoveNext
'Loop
'----------------End ADO from net---------------------------

'----------------------------------------------
'Store the returned text from query
'----------------------------------------------

strEventMessage = objRecordset.Fields.Item("Message")

WScript.Echo strEventMessage

objFile1.WriteLine(strEventMessage)

'close files after reading and writing.
objFile1.Close

--
Patrick Roye
Managed Services Dept.
Main Street Software, Inc.
.



Relevant Pages

  • trouble with SWbemDateTime conversion to win2k compatible.
    ... 'Prepare text files for query ... Const ForReading = 1 ... dtmEndDate = CDate ... 'Write header line for proper reading and searching. ...
    (microsoft.public.windows.server.scripting)
  • Re: SWbemDateTime to Win 2k Compatible
    ... The local time zone bias is read from the local registry. ... 'Prepare text files for query ... Const ForReading = 1 ... dtmEndDate = CDate ...
    (microsoft.public.scripting.vbscript)
  • Re: optimizer behaviour changed
    ... Given you have also done an order by which is not in the select list - the server must build an index to reorder the data which he is reading directly from the index. ... QUERY PLAN FOR STATEMENT 1. ... Using I/O Size 2 Kbytes for index leaf pages. ... With LRU Buffer Replacement Strategy for index leaf pages. ...
    (comp.databases.sybase)
  • Re: Programming in standard c
    ... may grow larger between the query and the reading. ... That wider programming environment can provide guarantees ... and learned my craft in the days when memory was ...
    (comp.lang.c)
  • Re: Continuous Form and Query
    ... That makes more sense than the way I was reading it. ... I set my query up to look like this in sql: ... Barry Gilbert wrote: ... "Wes H." ...
    (microsoft.public.access.forms)