You do something very weird with your for do while false next loop
. The early escape can also be done with the for loop itself:
Dim fout, WshShell
Set WshShell = WScript.CreateObject("WScript.Shell") ' Get this out of the loop, you only have to create it once.
For fout = 1 To 5
if fout = 5 then exit for
Call WshShell.LogEvent(1, "Test Event")
next
I would recommend using Option Explicit
. You use lots of variables I never see initialized. Furthermore, you use ForReading
this is not an VBScript keyword, you have to assign it the proper value. You can make it a Global Constant:
Const ForReading = 1
You would have spotted that if you used Option Explicit.
Try to make function that will work on their parameters and not external global variables. So refactor the EventCount
function to:
Function EventCount(logFilePath)
dim strData, arrLines
strData = objFSO.OpenTextFile(logFilePath, ForReading).ReadAll
arrLines = Split(strData,vbCrLf)
EventCount = UBound(arrLines) + 1
End Function
Did you notice that fout
is never used in the EventCount function?
You also want to increment the EventCount with 1, because an array is zero indexed (ubound will return -1 for empty arrays). Count is alway defined as the exact amount of items in contrary to the index.
EDIT: reading your logic, I think you want to do this:
Dim fout, objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Ping the website five times
For fout = 1 to 5
If PingSite(strWebsite) Then Exit For 'Website is pingable - no further action required
' The website was not pingable, write it to a logfile
RecordSingleEvent
' The website was not pingable five times in a row, log it to the eventlogger
If fout = 5 then
Set WshShell = WScript.CreateObject("WScript.Shell")
Call WshShell.LogEvent(1, "Test Event")
End if
Next
' We are done.
WScript.Quit