2

您好脚本专家,

我在远程服务器上有一个日志文件.. 在远程服务器中c:\vb\text.log

我已将我的远程系统包含在list.Txt类似

server1
server2

下面是日志的示例..

application working
[10/23/2012 working

[10/24/2012 nos appdown
error found you need to check this

下面是我的脚本。

Set Fso = CreateObject("Scripting.FileSystemObject")
Set InFile = fso.OpenTextFile("list.Txt")
Set out = fso.CreateTextFile("error.log")

Const ForReading = 1
Do While Not (InFile.atEndOfStream)
  strComputer = InFile.ReadLine
  today = Date()
  Set fso = CreateObject("Scripting.FileSystemObject")
  strFilePath = "\\" & strComputer & "\c$\vb\"

  Set InputFile = fso.OpenTextFile(strFilePath & "text.log", 1)
  Do While Not (InputFile.AtEndOfStream)
    strLine = InputFile.ReadLine

    If Left(line, Len(today)+1) = "[" & today Then
      ' line timestamped with today's date
      If InStr(line, "nos") > 0 Then
        ' line contains "error"
        out.WriteLine InStr & vbTab & strComputer
      End If
    End If
  Loop
  InputFile.close
Loop

out.Close
InFile.Close

基本上,上面的脚本应该从当前日期行仅从text.log. [10/24/2012 nos appdown然后如果在当前日期行中发现为“Nos”..那么它应该写入error.log带有计算机名称。

在我的情况下,输出没有出现,但看起来它正在搜索字符串“Nos”。

请让我摆脱这种情况......

4

2 回答 2

3

错误是您没有指定显式选项。像这样,

option explicit

这将迫使 VBScript 抱怨未声明的变量。通过这样做,您可以轻松地发现拼写错误的变量名。带有 dim 语句的 Delcare 变量,就像这样

dim Fso, out

再次运行脚本,发现您在比较中使用了一个不存在且未初始化的变量:

strLine = InputFile.ReadLine ' Read stuff to strLine
If Left(line, Len(today)+1) = "[" & today Then ' ERROR. line has no value!
于 2012-10-24T06:50:01.060 回答
0

您对我的脚本的改编存在几个问题:

  • 正如vonPryz已经指出的那样,这是问题的原因:

    strLine = InputFile.ReadLine
    If Left(line, Len(today)+1) = "[" & today Then

    当您将变量名称从file更改为时,strFile您必须更改该变量的每次出现,而不仅仅是分配它的行。

  • out.WriteLine InStr & vbTab & strComputer

    此行也会失败,因为InStr它是一个函数,并且您没有使用正确数量的参数调用它。

  • today = Date()

    这不应该在循环中,除非您希望在脚本运行期间更改日期并且需要在每个循环周期中都有当前日期。

  • Set fso = CreateObject("Scripting.FileSystemObject")

    fso在脚本的开头实例化。无需重新实例化它,尤其是在每个循环周期中。那只是浪费资源。

  • Const ForReading = 1

    当您从不使用常量时,定义常量是没有意义的。

  • Do While Not ...

    使用Do Until ...会更容易阅读和理解。

于 2012-10-24T11:34:33.100 回答