|
'需求:
'1.读取.txt文件(里面存放的是一些注册表的键值,包括名称、类型、数据)里面每行,然后去注册表里面的查找,看看是否匹配.
'2.然后输出到一个txt文件或者html文件,分别mathed和notmatched列。
'用法:
'1.将 strFileName 值修改为txt文件的位置, 在txt文件中,每行应该一项相应的注册表信息,依次为 "注册表键值名"<tab>"数据类型"<tab>"键值"如: "HKEY_CURRENT_USER\test\asdf REG_SZ asdf"
'2.如果注册表键值数据类型为二进制,即REG_BINARY型,需要手工进行转换, 如注册表值为 "00 01 02 03 09 0A 0F 11 1F"则应对应为"000102030910151731"
'3.如果注册表的键值为空,则需要用NULL来代替,如果本来值为"NULL", 则需要设置为"NULL", 如 "HKEY_CURRENT_USER\Console\FaceName Reg_sZ "NULL"" 或者 "HKEY_CURRENT_USER\Console\FaceName Reg_sZ NULL"
'4.生成的html文件为当前txt文件的同级目录下,名为 Result.html
On Error Resume Next
Const HKEY_CLASSES_ROOT = 2147483648 '''HKCR
Const HKEY_CURRENT_USER = 2147483649 '''HKCU
Const HKEY_LOCAL_MACHINE = 2147483650 '''HKLM
Const HKEY_Users = 2147483651 '''HKU
Const HKEY_Current_Config = 2147483653 '''HKCC
Const HKEY_DYN_DATA = 2147483654 '''HKDD
Const REG_SZ = 1 '''字符串型
Const REG_EXPAND_SZ = 2 '''扩展字符串型
Const REG_BINARY = 3 '''二进制型
Const REG_DWORD = 4 '''双字节型
Const REG_MULTI_SZ = 7 '''多字符串型
strFileName = "C:\test.txt" '存放注册表信息的地方
'格式化输出
strHTMLHead = "<html><head><title>比较结果</title></head><body><font size=5>文件来源: " + strFileName + ", 比较结果如下:</font><br><br><br><table border=2><tr><td><b>注册表键值名</b></td><td><b>数据类型</b></td><td><b>键值</b></td><td><b>比较结果</b></td></tr>"
strHTMLBody = ""
strHTMLEnd = "</table></body></html>"
Set WshShell = CreateObject("WScript.Shell")
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
Set FSO = CreateObject("scripting.filesystemobject")
Set FS = FSO.OpenTextFile(strFileName)
Do While Not(FS.AtEndOfLine)
str = FS.ReadLine
strRegValue = GetRegText(str)
strMyString = Left(strRegValue,(Len(strRegValue) - 1)) '去掉strRegValue中的最后一个"|"符号
s = Split(strMyString, "|")
boolCheckResult1 = CheckType(s(0), s(1))
If boolCheckResult1 Then
boolCheckResult2 = CheckValue(s(0), s(1), s(2))
End If
boolCheckResult = boolCheckResult1 And boolCheckResult2
FormatResult s(0), s(1), s(2), boolCheckResult
Loop
FS.Close
strFilePath = FSO.GetParentFolderName(strFileName)
strResultFile = strFilePath & Year(Date) & Month(Date) & Day(Date) & "-" & Hour(Time) & Minute(Time)& Second(Time) & "-Result.html"
Set FS2 = FSO.OpenTextFile(strResultFile, 2, True)
FS2.Write(strHTMLHead + strHTMLBody + strHTMLEnd)
FS2.Close
Set FS = Nothing
Set FS2 = Nothing
Set WshShell = Nothing
Set oReg = Nothing
Function GetRegText(str)
strString = ""
Set regEx = New RegExp
ss = "([\S]*)\s([\S]*)\s([\S]*)$"
RegEx.Global = True
RegEx.IgnoreCase = True
RegEx.Pattern = ss
Set objReg = RegEx.Execute(str)
For Each objSubReg In objReg
For Each strValue In objSubReg.Submatches
If strValue = "NULL" Then
strValue = ""
ElseIf strValue = """NULL""" Then
strValue = "NULL"
End If
strString = strString + strValue + "|"
Next
Next
Set RegEx = Nothing
GetRegText = strString
End Function
Function CheckType(strString, strType)
CheckType = False
ss = "([^\\]+)\\(.*)\\(.*)"
Set regEx = New RegExp
regex.Global = True
regEx.IgnoreCase = True
regEx.Pattern = ss
strKeyRoot = regEx.Replace(strString, "$1")
strKeyPath = regEx.Replace(strString, "$2")
arrValueName = regEx.Replace(strString, "$3")
Select Case UCase(strKeyRoot)
Case "HKEY_CLASSES_ROOT"
KeyRoot = HKEY_CLASSES_ROOT
Case "HKEY_CURRENT_USER"
KeyRoot = HKEY_CURRENT_USER
Case "HKEY_LOCAL_MACHINE"
KeyRoot = HKEY_LOCAL_MACHINE
Case "HKEY_USERS"
KeyRoot = HKEY_Users
Case "HKEY_CURRENT_CONFIG"
KeyRoot = HKEY_Current_Config
Case "HKEY_DYN_DATA"
KeyRoot = HKEY_DYN_DATA
End Select
Select Case UCase(strType)
Case "REG_SZ"
arrValueType = REG_SZ
Case "REG_EXPAND_SZ"
arrValueType = REG_EXPAND_SZ
Case "REG_BINARY"
arrValueType = REG_BINARY
Case "REG_DWORD"
arrValueType = REG_DWORD
Case "REG_MULTI_SZ"
arrValueType = REG_MULTI_SZ
End Select
oReg.EnumValues KeyRoot, strKeyPath, arrValueNames, arrValueTypes
If IsNull(arrValueTypes) Then
CheckType = False
Exit Function
End If
For i=0 To UBound(arrValueNames)
If arrValueNames(i) = arrValueName Then
If arrValueTypes(i) = arrValueType Then
CheckType = True
Exit Function
Else
CheckType = False
Exit Function
End If
End If
Next
Set regEx = Nothing
End Function
Function CheckValue(strRegPath, strType, strRegValue)
CheckValue = False
strActualValue = WshShell.RegRead(strRegPath)
If strType <> "REG_BINARY" Then
If CStr(strActualValue) = CStr(strRegValue) Then
CheckValue = True
Exit Function
End If
Else '判断为二进制时是否相等
boolTempResult1 = True
boolTempResult2 = True
For i = 0 To UBound(strActualValue)
strValue = CInt(Right(Left(strRegValue, 2*(i+1)), 2))
If strValue <> strActualValue(i) Then
boolTempResult2 = False
End If
Next
CheckValue = boolTempResult1 And boolTempResult2
Exit Function
End If
End Function
Function FormatResult(strString1, strString2,strString3, boolResult)
If boolResult Then
strHTMLBody = strHTMLBody + "<tr bgcolor=Lime><td>" + strString1 + "</td><td>" + strString2 + "</td><td>" + strString3 + "</td><td>" + "匹配</td></tr>"
Else
strHTMLBody = strHTMLBody + "<tr bgcolor=red><td>" + strString1 + "</td><td>" + strString2 + "</td><td>" + strString3 + "</td><td>" + "不匹配</td></tr>"
End If
End Function
如上
[ 本帖最后由 风雪夜归人 于 2010-5-7 11:22 编辑 ] |
|