如何将日志添加到我的 vbscript [英] how to add a log to my vbscript

查看:32
本文介绍了如何将日志添加到我的 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  = "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

有人可以帮我吗谢谢

推荐答案

有几种方法可以做到这一点.最简单的方法是在不修改脚本的情况下使用 cscript.exe(在命令提示符中)调用脚本并将输出重定向到一个文件:

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)

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

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

myLog.Close

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

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.

无耻的插件: 前段时间我厌倦了一遍又一遍地编写相同的样板日志功能,所以我写了一个 logger 类 封装了常用的日志记录工具(交互式控制台、文件、事件日志)并提供 4 个日志级别(错误、警告、信息、调试)的日志记录方法.该类可用于记录到这样的文件:

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.

这篇关于如何将日志添加到我的 vbscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆