how to add a log to my vbscript

前端 未结 2 1558
死守一世寂寞
死守一世寂寞 2020-12-02 02:06

i have this script that reads a list of computers and check to see if the computers have the right software version install. the script echo to me the computers with the wro

相关标签:
2条回答
  • 2020-12-02 02:40

    Why not use the system's event log? I described how in this answer

    It means most of the work is done for you and you don't need to worry about where to put your log file

    0 讨论(0)
  • 2020-12-02 02:42

    There are several ways to do this. The simplest way, without any modification to your script, would be to call the script with cscript.exe (in a command prompt) and redirect the output to a file:

    cscript your.vbs > output.log
    

    However, if you want a log to be created even when users double-click your script you'll have to change your script so that it writes to a file instead of echoing the output. Open the log file at the beginning of the script:

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

    replace WScript.Echo ... with myLog.WriteLine ..., and close the file before you exit from the script:

    myLog.Close
    

    A somewhat more sophisticated approach would be to create a set of logging functions, which will allow you create log lines depending on certain conditions, e.g. LogInfo() for informational log messages and LogError() for errors.

    Shameless plug: Some time ago I got fed up with writing the same boilerplate logging functions over and over again, so I wrote a logger class that encapsulates the usual logging facilities (interactive console, files, eventlog) and provides logging methods for 4 log levels (Error, Warning, Information, Debug). The class can be used for logging to a file like this:

    Set myLog = New CLogger
    myLog.LogToConsole = False
    myLog.LogFile = "C:\my.log"
    
    myLog.LogInfo "info message"
    ...
    myLog.LogError "an error occurred"
    

    The log file is automatically closed when the object is released.

    0 讨论(0)
提交回复
热议问题