How to add a log to my vbscript

I have this script that reads a list of computers and checks to see if the correct software versions are installed on the computer. the script gives me 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 = "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 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 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 someone help me, thanks

+7
source share
2 answers

There are several ways to do this. The easiest way, without making any changes to your script, is to call the script using cscript.exe (on the command line) and redirect the output to a file:

 cscript your.vbs > output.log 

However, if you want the log to be created, even if users double-click on your script link, you will have to change your script so that it is written to the file instead of repeating 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 exiting the script:

 myLog.Close 

A slightly more complex approach would be to create a set of logging functions that will allow you to create log lines depending on certain conditions, for example. LogInfo() for informational log messages and LogError() for errors.

Shameless plugin: Some time ago, I was tired of writing the same calendar functions with tables again and again, so I wrote a logger class that encapsulates the usual logging tools (interactive console, files, event logs) and provides logging methods for 4 levels log (error, warning, information, debugging). The class can be used to log in a file as follows:

 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 freed.

+14
source

Why not use a system event log? I described how in this answer

This means that most of the work is done for you, and you don’t have to worry about where to put the log file.

+2
source

All Articles