VBScript, WMI, End Loop

Posted on 2008-11-14
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
    LVL 9

    Accepted Solution

    "Exit For" is the command you want.
    LVL 9

    Assisted Solution

    Something like this:
    ii = ii + 1
    If ii = 10 Then Exit For

    Open in new window


    Author Comment

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

    Author Comment

    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

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

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
    Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now