如何在我的VBScript中添加日志

9

我有一个脚本,它会读取计算机清单并检查计算机是否安装了正确的软件版本。该脚本会向我回显具有错误版本的计算机,但我想改为记录日志。

Dim strComputer, objFSO, ObjShell, strDisplayName, objList, strObject
Dim objReg, arrSubKeys, strProduct, strVersion, strReqVersion
Const For_Writing = 2
Const ForReading = 1
const ForAppending = 3
Const HKLM        = &H80000002
Const strKeyPath  = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
strReqVersion = "8.2.1 MP2"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objList = objFSO.OpenTextFile("c:\test\test.txt",ForReading)
Do While Not objList.AtEndOfStream
    strComputer = objList.ReadLine
    If HostOnline(strComputer) = True Then 
       Inventory(strComputer)
    End If 
Loop 
Function Inventory(strComputer)
Set objTextFile = objFSO.OpenTextFile("c:\test\inventory.txt",2,true)
'creating a dictionary object
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
     strComputer & "\root\default:StdRegProv")
     ' Enumerate the subkeys of the Uninstall key
    objReg.EnumKey HKLM, strKeyPath, arrSubKeys
    For Each strProduct In arrSubKeys
  ' Get the product's display name
        objReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayName", strDisplayName
  ' Process only products whose name contain 'symantec'
        If InStr(1, strDisplayName, "Symantec", vbTextCompare) > 0 Then
    ' Get the product's display version
        objReg.GetStringValue HKLM, strKeyPath & "\" & strProduct, "DisplayVersion", strVersion
        If strReqVersion <> strVersion Then
            WScript.Echo strObject
            objDictionary.Add strComputer, strVersion
            For Each strObject In objDictionary
             WScript.Echo strObject

             objTextFile.WriteLine(strObject)
            Next   
            objTextFile.Close               
         End If    
        End If  
    Next
End Function
Function HostOnline(strComputername)
'---------- Test to see if host or url alive through ping -----------------
' Returns True if Host responds to ping
' 
' strComputername is a hostname or IP 
    Const OpenAsASCII = 0 
     Const FailIfNotExist = 0 
     Const ForReading =  1 
     Dim objShell, objFSO, sTempFile, fFile 
    Set objShell = CreateObject("WScript.Shell") 
     Set objFSO = CreateObject("Scripting.FileSystemObject") 
    sTempFile = objFSO.GetSpecialFolder(2).ShortPath & "\" & objFSO.GetTempName 
    objShell.Run "cmd /c ping -n 2 -l 8 " & strComputername & ">" & sTempFile, 0 , True 
    Set fFile = objFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsASCII) 
    Select Case InStr(fFile.ReadAll, "TTL=") 
         Case 0
            HostOnline = False 
         Case Else
            HostOnline = True 
    End Select 
    ffile.close 
     objFSO.DeleteFile(sTempFile)
    Set objFSO = Nothing
    Set objShell = Nothing
End Function

can some one help me please thanks

2个回答

21

有几种方法可以做到这一点。最简单的方法是不对脚本进行任何修改,只需在命令提示符中使用cscript.exe调用脚本,并将输出重定向到文件:

有几种方法可以做到这一点。最简单的方法是不对脚本进行任何修改,只需在命令提示符中使用cscript.exe调用脚本,并将输出重定向到文件:

cscript your.vbs > output.log

然而,如果您希望在用户双击脚本时创建日志,您需要更改脚本的写入方式,使其写入文件而不是输出。在脚本开始时打开日志文件:

Set myLog = objFSO.OpenTextFile("C:\my.log", For_Writing, True)

WScript.Echo ... 替换为 myLog.WriteLine ...,并在退出脚本之前关闭文件:

myLog.Close

一种更加复杂的方法是创建一组日志记录函数,这将允许您根据某些条件创建日志行,例如 LogInfo() 用于信息日志消息,LogError() 用于错误。

自我推销: 有一段时间我厌倦了一遍又一遍地编写相同的样板日志记录函数,因此我编写了一个日志记录器类,它封装了常规日志记录设施(交互式控制台、文件、事件日志)并提供了4个日志级别的记录方法(错误、警告、信息、调试)。可以像这样使用该类来记录到文件中:

Set myLog = New CLogger
myLog.LogToConsole = False
myLog.LogFile = "C:\my.log"

myLog.LogInfo "info message"
...
myLog.LogError "an error occurred"

当对象被释放时,日志文件会自动关闭。


谢谢Ansgar,我该如何在我的脚本中实现你的日志记录器类? - user1766952
将类代码复制粘贴到您的脚本中,并按上述说明使用它。 - Ansgar Wiechers

3
为什么不使用系统事件日志呢?我在这个答案中描述了如何做。
这意味着您大部分工作已经完成,您不需要担心在哪里放置您的日志文件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接