Hi,
I've been working on a script that reads the contents of a .REG file and converts it to entire key's so it can check the keys agains the registry.
there's just one bug in it: it will fail on DWORD value's even if there correct.
hope someone can shed some light on why it fails on DWORD's.
otherwise, enjoy!
regards,
jeroen
I've been working on a script that reads the contents of a .REG file and converts it to entire key's so it can check the keys agains the registry.
there's just one bug in it: it will fail on DWORD value's even if there correct.
Code:
'Constants
'=============================================================================================
CONST RED = 3
CONST ORANGE = 2
CONST GREEN = 1
CONST HKLM = &H80000002
CONST HKCR = &H80000000
CONST HKCU = &H80000001
CONST HKUSERS = &H80000003
CONST REG_SZ = 1
CONST REG_EXPAND_SZ = 2
CONST REG_BINARY = 3
CONST REG_DWORD = 4
CONST REG_MULTI_SZ = 7
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Lvl1 = 1
Lvl2 = 2
strComputerName = "."
'Set Objects
'==============================================================================================
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("wscript.shell")
Set objMsg = WScript.CreateObject("CDO.Message")
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputerName & "\root\cimv2")
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputerName & "/root/default:StdRegProv")
Set objFSO = CreateObject("Scripting.FileSystemObject")
ChkRegFile "U:\test.reg"
Function ChkRegFile(strFile)
If objFSO.FileExists(strFile) Then
Set objFile = objFSO.GetFile(strFile)
Set objInputFile = objFile.OpenasTextStream(ForReading, -2)
If Not objInputFile.AtEndOfStream Then
objInputFile.SkipLine
Else
Wscript.Echo "Already at the end of the file."
WScript.Quit
End If
Do While Not objInputFile.atEndOfStream
line = objInputFile.readline
strQuoteEqual = instr(line, """=")
strhex = instr(line, """")
strSlash = right(line, 1)
strLBracket = left(line, 1)
strRBracket = right(line, 1)
strBracket = strLBracket & strRBracket
If NOT line = "" Then
If strBracket = "[]" Then
strEdit = line
strEdit = right(strEdit, len(strEdit) - 1)
strEdit = left(strEdit, len(strEdit) - 1)
strMainKey = strEdit & "\" '"""" &
Else
If strQuoteEqual AND NOT strSlash = "\" Then
arrEdit = Split(line, """=", -1, 1)
strSubEdit = right(arrEdit(0), len(arrEdit(0)) - 1)
If instr(arrEdit(1), "\\") Then
arrEdit(1) = Replace(arrEdit(1), "\\", "\")
End If
If instr(arrEdit(1), "hex:") Then
arrSub = Split(arrEdit(1),":", -1, 1)
arrEdit(1) = Replace(arrSub(1), "\\", "\")
arrEdit(1) = Replace(arrEdit(1), ",", " ")
End If
If instr(arrEdit(1), "dword:") Then
arrEdit(1) = Replace(arrEdit(1), "dword:", "")
arrEdit(1) = HextoDec(arrEdit(1))
End If
strLQuotes = left(arrEdit(1), 1)
strRQuotes = right(arrEdit(1), 1)
strQuotes = strLQuotes & strRQuotes
If strQuotes = """""" Then
strValue = left(arrEdit(1), len(arrEdit(1)) - 1)
strValue = right(strValue, len(strValue) - 1)
arrEdit(1) = strValue
End If
strSubKey = strSubEdit & ";" & arrEdit(1)
End If
If strSlash = "\" or strHex = 0 Then
If strQuoteEqual Then
arrHexEdit = Split(line, """=", -1, 1)
strHexEditR = right(arrHexEdit(0), len(arrHexEdit(0)) - 1) '& """"
arrHex2 = Split(arrHexEdit(1),":", -1, 1)
strHexEditL = left(arrHex2(1), len(arrHex2(1)) - 1)
strHexEditL = Replace(strHexEditL, ",", " ")
strHexKey = strHexEditR & "," & strHexEditL
Else
strEditLine = right(line, len(line) - 2)
If strSlash = "\" Then
strEditLine = left(strEditline, len(strEditline) - 1)
End If
strEditLine = Replace(strEditLine, ",", " ")
strHexKey = strHexKey & strEditLine
End If
Else
strKeyF = strHexKey
strHexKey = ""
End If
If strSubKey <> "" Then
strTotalKey = strMainKey & strSubKey
strSplitKey = Split(strTotalKey, ";", -1, 1)
ChkRegValue strSplitKey(0) , strSplitKey(1), GREEN, RED
strSubKey = ""
End If
If strKeyF <> "" Then
strTotalKey = strMainKey & strKeyF
strSplitKey = Split(strTotalKey, ";", -1, 1)
ChkRegValue strSplitKey(0) , strSplitKey(1), GREEN, RED
End If
End If
End If
Loop
Else
Wscript.Echo "File " & strFile & "doesn't exist"
End If
End Function
Function ChkRegValue(strRegKey, data, Lvl1, Lvl2)
On Error Resume Next
If Right(strRegKey, 1) = "\" Then
strRegKey = Left(strRegKey, Len(strRegKey) - 1)
End If
aTmp = Split(strRegKey, "\", -1, 1)
sHK = aTmp(0)
sKey = aTmp(Ubound(aTmp))
SRest = Replace(Replace(strRegKey, aTmp(0), ""), sKey, "")
sRest = right(SRest, len(sRest) - 1)
SRest = left(sRest, len(sRest) - 1)
If sHK = "HKLM" or sHK = "HKEY_LOCAL_MACHINE" Then sHK = HKLM
If sHK = "HKCU" or sHK = "HKEY_CURRENT_USER" Then sHK = HKCU
If sHK = "HKCR" or sHK = "HKEY_CLASSES_ROOT" Then sHK = HKCR
If sHK = "HKUSERS" or sHK = "HKEY_USERS" Then sHK = HKUSERS
objReg.EnumValues sHK, sRest, arrValueNames, arrValueTypes
For i = 0 To UBound(arrValueNames)
if Ucase(arrValueNames(i)) = UCase(sKey) Then
Select Case arrValueTypes(i)
Case REG_SZ
objReg.GetStringValue sHK, sRest, arrValueNames(i), strValue
Case REG_EXPAND_SZ
objReg.GetExpandedStringValue sHK, sRest, arrValueNames(i), strValue
Case REG_BINARY
objReg.GetBinaryValue sHK, sRest, arrValueNames(i), arrValue
For Each value In arrValue
value = HexByte(value)
strValue = strValue & " " & value
Next
strValue = right(strValue, len(strValue) - 1)
Case REG_DWORD
objReg.GetDWORDValue sHK, sRest, arrValueNames(i), strValue
wscript.echo "DWORD! " & strValue
Case REG_MULTI_SZ
objReg.GetMultiStringValue sHK, sRest, arrValueNames(i), strValue
End Select
End If
Next
If strValue = UCase(data) OR strValue = LCase(data) OR strValue = data Then
logtxt = "Value: " & strRegKey & " is correct: " & strValue
Loglvl Lvl1, logtxt
wrlog logtxt
Else
logtxt = "Value: " & strRegKey & " is incorrect: " & strValue & ". Should be: " & data
Loglvl Lvl2, logtxt
wrlog logtxt
End If
End Function
Function HexByte(b)
HexByte = Right("0" & Hex(b), 2)
End Function
Function wrlog(strLogtxt)
wscript.echo strLogtxt
End Function
Function HexToDec(strHex)
dim lngResult
dim intIndex
dim strDigit
dim intDigit
dim intValue
lngResult = 0
for intIndex = len(strHex) to 1 step -1
strDigit = mid(strHex, intIndex, 1)
intDigit = instr("0123456789ABCDEF", ucase(strDigit))-1
if intDigit >= 0 then
intValue = intDigit * (16 ^ (len(strHex)-intIndex))
lngResult = lngResult + intValue
else
lngResult = 0
intIndex = 0 ' stop the loop
end if
next
HexToDec = lngResult
End Function
hope someone can shed some light on why it fails on DWORD's.
otherwise, enjoy!
regards,
jeroen