Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array not working

Status
Not open for further replies.

ipeca

IS-IT--Management
Jan 11, 2007
17
US
I'm trying to automatically backup my log files and the script seems to work until it starts the

For Each objLogfile in colLogFiles portion of the script. I get an0x8004107 error and it spits back a null value.

So I'm guessing the evtLog(i) variable in this set method is wrong but after spending the morning trying to figure it out....I'm missing some hair...have broken half a dozen pencils...ya know how it is.

Set colLogFiles = objWMIService.ExecQuery _
("Select * from Win32_NTEventLogFile where LogFileName=' ' & evtLog(i)")



Can anyone help out a noob?

Code:
Option Explicit
'On Error Resume Next

'Declared Variables

Dim strComputer
Dim log
Dim objLogfile
Dim dtmThisDay, dtmThisMonth, dtmThisYear
Dim strBackupName
Dim evtLog
Dim colLogFiles
Dim objWMIService
Dim errBackupLog
Dim i

'Setting variables

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate,(Backup)}!\\" & _
        strComputer & "\root\cimv2")
dtmThisDay = Day(Date)
dtmThisMonth = Month(Date)
dtmThisYear = Year(Date)
strBackupName = dtmThisYear & "_" & dtmThisMonth & "_" & dtmThisDay
evtLog = Array("Application", "Securtity", "System")
i = 0


'Include event logs in a array to be used later in the script
Set colLogFiles = objWMIService.ExecQuery _
    ("Select * from Win32_NTEventLogFile where LogFileName=' ' & evtLog(i)")

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate,(Backup,security)}!\\" & _
        strComputer & "\root\cimv2")


'Begin backup of event logs

For i = 0 To 2
	Wscript.Echo("starting 1 " & evtLog(i)) 'troubleshooting process

		 For Each objLogfile in colLogFiles
			Wscript.Echo("starting 2") 'troubleshooting process
			 errBackupLog = objLogFile.BackupEventLog("C:\Documents and Settings\macneill\My Documents\logs\evtLog(i)" & strBackupName & ".evt")
					Wscript.Echo("starting 3") 'troubleshooting process
			 If errBackupLog <> 0 Then        
        			Wscript.Echo "The evtLog(i) event log could not be backed up."	
   			 End If
		Next
Wscript.Echo("starting pass 4") 'troubleshooting process
Next
 
First, you have two "Set objWMIService =" lines; delete the first, and move the second one before the "Set colLogFiles =" line.

And I dont think you can use the array in the select statement like you have. You can remove the "WHERE", and just filter the log files you want with an IF statement;

Code:
strComputer = "."
[COLOR=green]'Set objWMIService = GetObject("winmgmts:" _
'    & "{impersonationLevel=impersonate,(Backup)}!\\" & _
'        strComputer & "\root\cimv2")[/color]
dtmThisDay = Day(Date)
dtmThisMonth = Month(Date)
dtmThisYear = Year(Date)
strBackupName = dtmThisYear & "_" & dtmThisMonth & "_" & dtmThisDay
'''evtLog = Array("Application", "Securtity", "System")
'''i = 0

[COLOR=red]'moved up[/color]
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate,(Backup,security)}!\\" & _
        strComputer & "\root\cimv2")

'Include event logs in a array to be used later in the script
Set colLogFiles = objWMIService.ExecQuery _
    ("Select * from Win32_NTEventLogFile")



'Begin backup of event logs

'''For i = 0 To 2
'''    Wscript.Echo("starting 1 " & evtLog(i)) 'troubleshooting process

	For Each objLogfile in colLogFiles
[COLOR=red]		If objLogfile.LogFileName = "Application" Or _
			objLogfile.LogFileName = "Security" Or _
			objLogfile.LogFileName = "System" Then[/color]
			
			Wscript.Echo("starting 2") 'troubleshooting process
			errBackupLog = objLogFile.BackupEventLog("C:\Documents and Settings\macneill\My Documents\logs\" & [COLOR=red]objLogfile.LogFileName[/color] & strBackupName & ".evt")
			Wscript.Echo("starting 3") 'troubleshooting process
			If errBackupLog <> 0 Then        
				Wscript.Echo "The " & [COLOR=red]objLogfile.LogFileName[/color] & " event log could not be backed up."    
			End If
		End If

	Next
 
all hail guitarzan !!!!!

seriously, thank you. and here I thought an array would be the way to go...should have stayed with KISS...

thanks again
 
What happen if, in your original post, you replace this:
("Select * from Win32_NTEventLogFile where LogFileName=' ' & evtLog(i)")
with this ?
Code:
("Select * from Win32_NTEventLogFile where LogFileName='" & evtLog(i) & "'")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
but i=0 at that point, so only the Application log would be returned. But if that line was moved inside the for loop at the bottom, that should work also.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top