生成每天的日志 .vbs

生成每天的日志 .vbs

我正在尝试用这个脚本创建一个日志文件

hostIp      = wscript.arguments(0)
logfilename = wscript.arguments(1)
Set fso     = CreateObject("Scripting.FileSystemObject")
Set Shell   = CreateObject("Wscript.Shell")
' OpenTextFile Method requires a Const value
' (Over)Write = 2  Append = 8  
    d = Day(Now) 
    m = Month(Now)  
    y = Year(Now)
    myDateFormat= d & "-" & m & "-" & y 
Set logfile = fso.OpenTextFile(logfilename & " " & myDateFormat & ".log", 8, True)
shellstring = "%comspec% /c ping -t -f -l 32 -w 1000 " & hostIP
Set oExec   = Shell.Exec(shellstring)
wscript.echo "Ping Error log With Timestamp - Ctrl + C to halt"
Do While oExec.StdOut.AtEndOfStream <> True
      pingline = Date & " " & Time & " " & oExec.StdOut.ReadLine
'      If InStr(pingline, "TTL=") = 0 Then
         logfile.WriteLine(pingline)
'      End If
Loop

我认为它没问题,但我已经运行了 3 天,只有一个文件,而不是 3 个。您知道这个脚本有什么问题吗?顺便说一下,我在 cmd 上使用以下行运行此脚本

FileName ip logname.log

答案1

这有帮助吗:

dim objShell : set ObjShell  = CreateObject("Wscript.Shell")
dim objFso   : set objFso    = CreateObject("Scripting.FileSystemObject")

dim shellstring : shellstring = "%comspec% /c ping -t -f -l 32 -w 1000 " & wscript.arguments(0)

dim oExec : set oExec = ObjShell.Exec(shellstring)

do while oExec.StdOut.AtEndOfStream <> true
  log( oExec.StdOut.ReadLine )
loop

function log(strLineIn)

  myDateFormat= Day(Now) & "-" & Month(Now) & "-" & Year(Now)

  dim logfile : set logfile = objFso.OpenTextFile(wscript.arguments(1) & " " & myDateFormat & ".log", 8, True)

  if instr(strLineIn, "TTL") > 0 then
    logfile.writeline date() & "-" & time() & ": " & strLineIn
  end if

  logfile.close

end function

相关内容