Function 写入错误并应创建事件错误的脚本
如果它工作,应该检查互联网连接,如果有连接,它什么也不做。如果没有连接,它应该在TXT文件中写入一个错误。如果发生5次,它应该创建一个错误,但它没有 我将向您展示我现在拥有的全部代码,以及我希望在循环中使用的代码片段。我不能以我想要的方式得到它。我希望它在写入文件5次后创建1个事件错误 这是全部代码,我将把我想要的代码放在它下面的一个循环中Function 写入错误并应创建事件错误的脚本,function,vbscript,while-loop,event-log,Function,Vbscript,While Loop,Event Log,如果它工作,应该检查互联网连接,如果有连接,它什么也不做。如果没有连接,它应该在TXT文件中写入一个错误。如果发生5次,它应该创建一个错误,但它没有 我将向您展示我现在拥有的全部代码,以及我希望在循环中使用的代码片段。我不能以我想要的方式得到它。我希望它在写入文件5次后创建1个事件错误 这是全部代码,我将把我想要的代码放在它下面的一个循环中 strDirectory = "Z:\text2" strFile = "\foutmelding.txt" strText = "De connectie
strDirectory = "Z:\text2"
strFile = "\foutmelding.txt"
strText = "De connectie is verbroken"
strWebsite = "www.helmichbeens.com"
If PingSite(strWebsite) Then WScript.Quit 'Website is pingable - no further action required
Set objFSO = CreateObject("Scripting.FileSystemObject")
RecordSingleEvent
Dim fout
For fout = 1 To 5 : Do
If fout = 5 Then Exit Do
Set WshShell = WScript.CreateObject("WScript.Shell")
Call WshShell.LogEvent(1, "Test Event")
Loop While False : next
'------------------------------------
'Record a single event in a text file
'------------------------------------
Sub RecordSingleEvent
If Not objFSO.FolderExists(strDirectory) Then objFSO.CreateFolder(strDirectory)
Set objTextFile = objFSO.OpenTextFile(strDirectory & strFile, 8, True)
objTextFile.WriteLine(Now & strText)
objTextFile.Close
End sub
'----------------
'Ping my web site
'----------------
Function PingSite( myWebsite )
Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
objHTTP.Open "GET", "http://" & myWebsite & "/", False
objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)"
On Error Resume Next
objHTTP.Send
PingSite = (objHTTP.Status = 200)
On Error Goto 0
End Function
'-----------------------------------------------
'Counts the number of lines inside the text file
'-----------------------------------------------
Function EventCount(fout)
strData = objFSO.OpenTextFile(strDirectory & strFile,ForReading).ReadAll
arrLines = Split(strData,vbCrLf)
EventCount = UBound(arrLines)
End Function
这就是全部代码,它不能正常工作,因为它会立即创建一个事件日志,并且应该在脚本向文本文件写入5次之后再创建
Sub RecordSingleEvent
If Not objFSO.FolderExists(strDirectory) Then objFSO.CreateFolder(strDirectory)
Set objTextFile = objFSO.OpenTextFile(strDirectory & strFile, 8, True)
objTextFile.WriteLine(Now & strText)
objTextFile.Close
End sub
下面是写入文本文件的代码
Sub RecordSingleEvent
If Not objFSO.FolderExists(strDirectory) Then objFSO.CreateFolder(strDirectory)
Set objTextFile = objFSO.OpenTextFile(strDirectory & strFile, 8, True)
objTextFile.WriteLine(Now & strText)
objTextFile.Close
End sub
这是代码,但这部分不起作用,或者至少我认为是这部分
Dim fout
For fout = 1 To 5 : Do
If fout = 5 Then Exit Do
Set WshShell = WScript.CreateObject("WScript.Shell")
Call WshShell.LogEvent(1, "Test Event")
Loop While False : next
Function EventCount(fout)
strData = objFSO.OpenTextFile(strDirectory & strFile,ForReading).ReadAll
arrLines = Split(strData,vbCrLf)
EventCount = UBound(arrLines)
End Function
这是不工作的部分,我不知道该做什么了,所以请你看看它,你非常
顺便说一句:这段代码对网络管理员非常有用你用你的
for do while false next loop做了一些非常奇怪的事情
。早期转义也可以通过for循环本身完成:
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
我建议使用选项Explicit
。你使用了很多我从未见过的变量。此外,如果使用进行读取
这不是VBScript关键字,则必须为其指定正确的值。您可以将其设置为全局常量:
Const ForReading = 1
如果使用Option Explicit,您会发现这一点
尽量使函数对其参数而不是外部全局变量起作用。因此,重构EventCount
函数以:
Function EventCount(logFilePath)
dim strData, arrLines
strData = objFSO.OpenTextFile(logFilePath, ForReading).ReadAll
arrLines = Split(strData,vbCrLf)
EventCount = UBound(arrLines) + 1
End Function
您是否注意到EventCount函数中从未使用fout
您还希望将EventCount增加为1,因为数组的索引为零(对于空数组,ubound将返回-1)。计数通常定义为与索引相反的项目的确切数量
编辑:阅读您的逻辑,我认为您希望这样做:
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
谢谢你的回答,它确实有效,但我发现它不是我要找的东西:P