如何将日志添加到我的vbscript
我有一个脚本,它读取计算机列表,并检查计算机是否安装了正确的软件版本。脚本向我回显了版本错误的计算机,但我想做一个日志如何将日志添加到我的vbscript,vbscript,Vbscript,我有一个脚本,它读取计算机列表,并检查计算机是否安装了正确的软件版本。脚本向我回显了版本错误的计算机,但我想做一个日志 Dim strComputer, objFSO, ObjShell, strDisplayName, objList, strObject Dim objReg, arrSubKeys, strProduct, strVersion, strReqVersion Const For_Writing = 2 Const ForReading = 1 const ForAppend
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
Dim strComputer、objFSO、ObjShell、strDisplayName、objList、strObject
Dim objReg、ARR子键、strProduct、strVersion、strReqVersion
写入常数=2
常数ForReading=1
出现的常数=3
常数HKLM=&H8000002
Const strKeyPath=“软件\Microsoft\Windows\CurrentVersion\Uninstall”
strReqVersion=“8.2.1 MP2”
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
设置objShell=CreateObject(“WScript.Shell”)
设置objList=objFSO.OpenTextFile(“c:\test\test.txt”,用于读取)
不执行objList.AtEndOfStream时执行此操作
strComputer=objList.ReadLine
如果HostOnline(strComputer)=True,则
库存(strc计算机)
如果结束
环
功能清单(strComputer)
设置objTextFile=objFSO.OpenTextFile(“c:\test\inventory.txt”,2,true)
'创建字典对象
Set objDictionary=CreateObject(“Scripting.Dictionary”)
设置objReg=GetObject(“winmgmts:{impersonationLevel=impersonate}!\\”和
strComputer&“\root\default:StdRegProv”)
'枚举卸载密钥的子密钥
objReg.EnumKey HKLM、strKeyPath、arrsubkey
对于子键中的每个strProduct
'获取产品的显示名称
objReg.GetStringValue HKLM,strKeyPath&“\”&strProduct,“DisplayName”,strDisplayName
'仅处理名称包含'symantec'的产品'
如果InStr(1,strDisplayName,“Symantec”,vbTextCompare)>0,则
'获取产品的显示版本
objReg.GetStringValue HKLM,strKeyPath&“\”&strProduct,“显示版本”,strrvision
如果是strReqVersion,则
WScript.Echo strObject
对象描述。添加strComputer、strrvision
对于objDictionary中的每个strObject
WScript.Echo strObject
objTextFile.WriteLine(strObject)
下一个
objTextFile.Close
如果结束
如果结束
下一个
端函数
函数HostOnline(strComputername)
'------------通过ping测试主机或url是否处于活动状态-----------------
'如果主机响应ping,则返回True
'
'strComputername是主机名或IP
常量OpenAsASCII=0
常数FailIfNotExist=0
常数ForReading=1
Dim objShell、objFSO、sTempFile、fFile
设置objShell=CreateObject(“WScript.Shell”)
设置objFSO=CreateObject(“Scripting.FileSystemObject”)
sTempFile=objFSO.GetSpecialFolder(2).ShortPath&“\”&objFSO.GetTempName
运行“cmd/cping-n2-l8”&strComputername&“>”&sTempFile,0,True
设置fFile=objFSO.OpenTextFile(sTempFile,ForReading,FailIfNotExist,OpenAsASCII)
选择Case InStr(fFile.ReadAll,“TTL=”)
案例0
HostOnline=False
其他情况
HostOnline=True
结束选择
关闭
objFSO.DeleteFile(sTempFile)
设置objFSO=Nothing
Set objShell=Nothing
端函数
有人能帮我吗谢谢有几种方法可以做到这一点。在不修改脚本的情况下,最简单的方法是使用
cscript.exe
(在命令提示符下)调用脚本,并将输出重定向到文件:
cscript your.vbs > output.log
但是,如果您希望在用户双击脚本时创建日志,则必须更改脚本,使其写入文件,而不是回显输出。打开脚本开头的日志文件:
Set myLog = objFSO.OpenTextFile("C:\my.log", For_Writing, True)
myLog.Close
将WScript.Echo…
替换为myLog.WriteLine…
,并在退出脚本之前关闭文件:
Set myLog = objFSO.OpenTextFile("C:\my.log", For_Writing, True)
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,我如何使用脚本实现您的logger类将类代码复制/粘贴到您的脚本中,并按上述方式使用它。