SWbemDateTime to Win 2k Compatible
- From: Viper640 <pat@xxxxxxxxxx>
- Date: Mon, 10 Dec 2007 08:59:05 -0800
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.
.
- Follow-Ups:
- Re: SWbemDateTime to Win 2k Compatible
- From: Richard Mueller [MVP]
- Re: SWbemDateTime to Win 2k Compatible
- Prev by Date: Re: Writing to StdErr
- Next by Date: Re: Drag and drop into an HTA
- Previous by thread: Drag and drop into an HTA
- Next by thread: Re: SWbemDateTime to Win 2k Compatible
- Index(es):
Relevant Pages
|