Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


VBScript, WMI, End Loop

Posted on 2008-11-14
Medium Priority
Last Modified: 2012-05-05
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:,"
    '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 & ","
            ii = ii + 1

Open in new window

Question by:mmitchell57
  • 3
  • 2

Accepted Solution

Mass Dot Net earned 2000 total points
ID: 22961667
"Exit For" is the command you want.

Assisted Solution

by:Mass Dot Net
Mass Dot Net earned 2000 total points
ID: 22961678
Something like this:
ii = ii + 1
If ii = 10 Then Exit For

Open in new window


Author Comment

ID: 22961878
Really, I just dump that fright before the "next" in the for loop i'm using to write the lines?

Author Comment

ID: 22961960
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.

Author Closing Comment

ID: 31516865
easy enough, it worked quite well. Thank you! :)

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question