Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
789 views
in Technique[技术] by (71.8m points)

how to add a log to my vbscript

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 wrong version, but i want to make a log instead

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  = "SOFTWAREMicrosoftWindowsCurrentVersionUninstall"
strReqVersion = "8.2.1 MP2"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objList = objFSO.OpenTextFile("c:estest.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:estinventory.txt",2,true)
'creating a dictionary object
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!" & _ 
     strComputer & "
ootdefault: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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...