Link to home
Start Free TrialLog in
Avatar of mmitchell57
mmitchell57

asked on

VBScript, WMI, End Loop

Hello. I am working to extra event logs from the server I manage. I wrote a WMI script to do this and write it to a CSV file. My problem is, I only want the most recent 10 errors to be written. Then I want it to exit the loop and move on to the next server. Below is snippet of the script I wrote. I figured I'd exit a loop if the a counter reached 10 or so but I can't figure out how ot get out of the loop.  Anyhow, any help is appreciated.

for i = 0 to cnt 
    strComputer = arr1(i)                                       'Gets Computer Name
    objfile1.writeline "Errors For Server: " & strComputer      'Writes header to each log
    
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate,(Security)}!\\" & strComputer & "\root\cimv2")     'Connects to CIMV2 DB
    Set colLogFiles = objWMIService.ExecQuery ("Select * from Win32_NTEventLogFile where LogfileName='System'")                     'Queries for System Log
    
    For Each objLogFile in colLogFiles
        intTotal = objLogFile.NumberOfRecords
        objFile1.Writeline "Total Number of records for server " & strComputer & ": " & intTotal
        objFile1.Writeline "Type:, EventCode:, TimeStamp:,"
        
    Next
    
    'set i = 0
    Set colLoggedEvents = objWMIService.ExecQuery ("Select * from Win32_NTLogEvent Where Logfile = 'System' AND Type = 'error'")
        for each objEvent in colLoggedEvents 
            objfile1.Write objEvent.Type & ", "
            objFile1.Write objEvent.EventCode & ", "
            dtmEventDate = objEvent.TimeWritten
            strTimeWritten = WMIDateStringToDate(dtmEventDate)
            objFile1.write strTimeWritten & ", "
            objFile1.write objEvent.Message & ","
            objFile1.Writeline
            ii = ii + 1
        next
    objFile1.writeline
Next

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Member_2_3718378
Member_2_3718378

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mmitchell57
mmitchell57

ASKER

Really, I just dump that fright before the "next" in the for loop i'm using to write the lines?
Ok, it appears the script is running, and it's not grown by mb's a second so it might be in a good direction. I'm waiting for it to finish.
easy enough, it worked quite well. Thank you! :)