0

这是我想在后台运行的代码,所以没有窗口消息。它的含义是它检查连接。如果没有连接,它会将错误写入文件。如果有 5 行,一个函数会读取该文件,它应该创建一个事件错误。问题是最后一部分不能正常工作。

我的问题是有人可以修复它或帮助我修复它。这是代码:

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
If EventCount >= 5 Then
    objFSO.DeleteFile strDirectory & strFile
    Set WshShell = WScript.CreateObject("WScript.Shell")
    strCommand = "eventcreate /T Error /ID 100 /L Scripts /D " & _
    Chr(34) & "Test event." & Chr(34)
    WshShell.Run strcommand
End if
'------------------------------------
'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()
    strData = objFSO.OpenTextFile(strDirectory & strFile,ForReading).ReadAll
    arrLines = Split(strData,vbCrLf)
    EventCount = UBound(arrLines)
    Set objFSO = Nothing
End Function

这就是您可以复制它以自己查看的代码。我感谢您的时间和兴趣

问候赫尔米奇

4

3 回答 3

1

It doesn't work because function EventCount sets objFSO=nothing, so,

If EventCount >= 5 Then
    objFSO.DeleteFile strDirectory & strFile

fails

于 2013-10-31T17:37:46.610 回答
0

使用 Shell 对象的 logevent 方法

If EventCount >= 5 Then
    objFSO.DeleteFile strDirectory & strFile
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Call WshShell.LogEvent(1, "Test Event")
End if

您不需要运行单独的命令

于 2013-10-31T14:11:57.450 回答
0

那不是问题是这个

Windows 主机脚本出错

Line:41 Char:2 Translation of error: the data required for this operation are not yet available code: 80070057 source: WinHttp.WinHttpRequest 这就是问题所在,我不知道如何解决

原因是他无法读取 txtfile 中的行,然后不执行 create event 命令

于 2013-11-01T09:17:12.990 回答