'====================================================================
'= Enumerate Registry Values VBScript sample - Copyright © 2007, Dave Moats
'=
'= This sample is provided 'as-is', without any express or implied warranty.
'= In no event will the authors be held liable for any damages arising from
'= the use of this sample code.
'=
'= Permission is granted to anyone to use this sample code for any purpose,
'= including commercial applications, subject to the following restrictions:
'=
'= The origin of this code must not be misrepresented;
'= you must not claim that you wrote the original code.
'= If you use this code, an acknowledgment in the
'= documentation is requested - shown below:
'=
'= Portions Copyright © 2007, Dave Moats (http://www.davemoats.com/).
'=
'====================================================================
'====================================================================
'== enum_reg_values.vbs - script to enumerate all the values
'== located under HKLM\SOFTWARE and show
'== what paths stored in the registry no
'== longer point to a valid file
'====================================================================
const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002
const REG_SZ = 1
const REG_EXPAND_SZ = 2
const REG_BINARY = 3
const REG_DWORD = 4
const REG_MULTI_SZ = 7
set fso = createobject ( "Scripting.FileSystemObject")
set wshShell = createobject ( "WScript.Shell")
set regObj = getobject ( "winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
keyPath = "SOFTWARE"
indentStr = " "
wscript.echo vbcrlf & "Enumeration of HKEY_LOCAL_MACHINE Key:" & vbcrlf
enumRegKeys HKEY_LOCAL_MACHINE, keyPath
set regObj = nothing
set wshShell = nothing
set fso = nothing
wscript.quit
'====================================================================
'== sub enumRegKeys - recursive sub used to enumerate the registry
'== keys
'====================================================================
sub enumRegKeys ( regKey, keyPath )
on error resume next
dim enumKeys
regObj.enumkey regKey, keyPath, enumKeys
for each subKey in enumKeys
if err then
err.clear
enumRegValues regKey, keyPath
exit sub
else
if subKey <> "Classes" then
enumRegKeys regKey, keyPath & "\" & subKey
end if
end if
if err then
err.clear
end if
next
if err then
err.clear
end if
end sub
'====================================================================
'== sub enumRegValues - enumerates all the registry values under
'== the run key in the registry
'====================================================================
sub enumRegValues ( regKey, keyPath )
on error resume next
regObj.EnumValues regKey, keyPath, arNames, arTypes
if err then
err.clear
exit sub
end if
if isnull ( arNames ) then
exit sub
end if
for i=0 to ubound ( arNames)
if arNames ( i) <> "" then
select case arTypes ( i)
case REG_SZ
regObj.GetStringValue regKey, keyPath, arNames ( i), regVal
if regVal <> "" and not isnull ( regVal) then
if checkValForPath ( regVal ) = 1 then
if not ( fso.fileexists ( getExePath ( regVal))) then
displayRegInfo keyPath, arNames ( i), regVal
end if
end if
end if
case REG_EXPAND_SZ
regObj.GetExpandedStringValue regKey, keyPath, arNames ( i), regVal
if regVal <> "" and not isnull ( regVal) then
if checkValForPath ( regVal ) = 1 then
if not ( fso.fileexists ( getExePath ( regVal))) then
displayRegInfo keyPath, arNames ( i), regVal
end if
end if
end if
case REG_MULTI_SZ
regObj.GetMultiStringValue regKey, keyPath, arNames ( i), arrValues
for each regVal in arrValues
if regVal <> "" and not isnull ( regVal) then
if checkValForPath ( regVal ) = 1 then
if not ( fso.fileexists ( getExePath ( regVal))) then
displayRegInfo keyPath, arNames ( i), regVal
end if
end if
end if
next
end select
end if
next
if err then
' wscript.echo "enumRegValues - " & err.description
err.clear
end if
end sub
'====================================================================
'== sub displayRegInfo - sub to wrap the wscript echo calls
'====================================================================
sub displayRegInfo ( regPath, regName, regVal )
wscript.echo regPath
wscript.echo indentStr & "Registry Value Name: " & regName
wscript.echo indentStr & "Registry Value Data: " & regVal
wscript.echo indentStr & "File Not Found"
end sub
'====================================================================
'== function checkValForPath - function used to determine if the
'== string contains a path to a file
'====================================================================
function checkValForPath ( regVal )
dim retVal
retVal = 0
if instr ( 1, lcase ( regVal), ".exe" ) <> 0 or instr ( 1, lcase ( regVal), ".dll" ) then retVal = 1
if instr ( 1, lcase ( regVal), "\" ) = 0 then retVal = 0
checkValForPath = retVal
end function
'====================================================================
'== function getExePath - function to parse out just the path from
'== the string, removing non essential stuff
'====================================================================
function getExePath ( regVal )
' remove any stuff that we don't want in the path
regVal = replace ( regVal, """", "" )
regVal = replace ( regVal, "@", "" )
regVal = replace ( regVal, "?", "" )
regVal = replace ( regVal, "file:///", "" )
regVal = replace ( regVal, "resource=", "" )
' expand any env settings
iFirst = instr ( 1, regVal, "%" )
iSecond = instr ( iFirst + 1, regVal, "%" )
if iFirst <> 0 and iSecond <> 0 then
envString = mid ( regVal, iFirst, iSecond )
expandedString = expandEnvString ( envString )
regVal = replace ( regVal, envString, expandedString )
end if
' not get rid of any command line options leaving just the
' path to the exe
iLoc1 = instr ( 1, lcase ( regVal), ".exe" )
iLoc2 = instr ( 1, lcase ( regVal), ".dll" )
if iLoc1 <> 0 then
regVal = mid ( regVal, 1, iLoc1 + 3 )
end if
if iLoc2 <> 0 then
regVal = mid ( regVal, 1, iLoc2 + 3 )
end if
getExePath = regVal
end function
'====================================================================
'== function expandEnvString - function to expand any environment
'== variables
'====================================================================
function expandEnvString ( envVal )
if isnull ( envVal ) or envVal = "" then
expandEnvString = ""
exit function
end if
expandEnvString = wshShell.expandenvironmentstrings ( envVal )
end function
'= Enumerate Registry Values VBScript sample - Copyright © 2007, Dave Moats
'=
'= This sample is provided 'as-is', without any express or implied warranty.
'= In no event will the authors be held liable for any damages arising from
'= the use of this sample code.
'=
'= Permission is granted to anyone to use this sample code for any purpose,
'= including commercial applications, subject to the following restrictions:
'=
'= The origin of this code must not be misrepresented;
'= you must not claim that you wrote the original code.
'= If you use this code, an acknowledgment in the
'= documentation is requested - shown below:
'=
'= Portions Copyright © 2007, Dave Moats (http://www.davemoats.com/).
'=
'====================================================================
'====================================================================
'== enum_reg_values.vbs - script to enumerate all the values
'== located under HKLM\SOFTWARE and show
'== what paths stored in the registry no
'== longer point to a valid file
'====================================================================
const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002
const REG_SZ = 1
const REG_EXPAND_SZ = 2
const REG_BINARY = 3
const REG_DWORD = 4
const REG_MULTI_SZ = 7
set fso = createobject ( "Scripting.FileSystemObject")
set wshShell = createobject ( "WScript.Shell")
set regObj = getobject ( "winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
keyPath = "SOFTWARE"
indentStr = " "
wscript.echo vbcrlf & "Enumeration of HKEY_LOCAL_MACHINE Key:" & vbcrlf
enumRegKeys HKEY_LOCAL_MACHINE, keyPath
set regObj = nothing
set wshShell = nothing
set fso = nothing
wscript.quit
'====================================================================
'== sub enumRegKeys - recursive sub used to enumerate the registry
'== keys
'====================================================================
sub enumRegKeys ( regKey, keyPath )
on error resume next
dim enumKeys
regObj.enumkey regKey, keyPath, enumKeys
for each subKey in enumKeys
if err then
err.clear
enumRegValues regKey, keyPath
exit sub
else
if subKey <> "Classes" then
enumRegKeys regKey, keyPath & "\" & subKey
end if
end if
if err then
err.clear
end if
next
if err then
err.clear
end if
end sub
'====================================================================
'== sub enumRegValues - enumerates all the registry values under
'== the run key in the registry
'====================================================================
sub enumRegValues ( regKey, keyPath )
on error resume next
regObj.EnumValues regKey, keyPath, arNames, arTypes
if err then
err.clear
exit sub
end if
if isnull ( arNames ) then
exit sub
end if
for i=0 to ubound ( arNames)
if arNames ( i) <> "" then
select case arTypes ( i)
case REG_SZ
regObj.GetStringValue regKey, keyPath, arNames ( i), regVal
if regVal <> "" and not isnull ( regVal) then
if checkValForPath ( regVal ) = 1 then
if not ( fso.fileexists ( getExePath ( regVal))) then
displayRegInfo keyPath, arNames ( i), regVal
end if
end if
end if
case REG_EXPAND_SZ
regObj.GetExpandedStringValue regKey, keyPath, arNames ( i), regVal
if regVal <> "" and not isnull ( regVal) then
if checkValForPath ( regVal ) = 1 then
if not ( fso.fileexists ( getExePath ( regVal))) then
displayRegInfo keyPath, arNames ( i), regVal
end if
end if
end if
case REG_MULTI_SZ
regObj.GetMultiStringValue regKey, keyPath, arNames ( i), arrValues
for each regVal in arrValues
if regVal <> "" and not isnull ( regVal) then
if checkValForPath ( regVal ) = 1 then
if not ( fso.fileexists ( getExePath ( regVal))) then
displayRegInfo keyPath, arNames ( i), regVal
end if
end if
end if
next
end select
end if
next
if err then
' wscript.echo "enumRegValues - " & err.description
err.clear
end if
end sub
'====================================================================
'== sub displayRegInfo - sub to wrap the wscript echo calls
'====================================================================
sub displayRegInfo ( regPath, regName, regVal )
wscript.echo regPath
wscript.echo indentStr & "Registry Value Name: " & regName
wscript.echo indentStr & "Registry Value Data: " & regVal
wscript.echo indentStr & "File Not Found"
end sub
'====================================================================
'== function checkValForPath - function used to determine if the
'== string contains a path to a file
'====================================================================
function checkValForPath ( regVal )
dim retVal
retVal = 0
if instr ( 1, lcase ( regVal), ".exe" ) <> 0 or instr ( 1, lcase ( regVal), ".dll" ) then retVal = 1
if instr ( 1, lcase ( regVal), "\" ) = 0 then retVal = 0
checkValForPath = retVal
end function
'====================================================================
'== function getExePath - function to parse out just the path from
'== the string, removing non essential stuff
'====================================================================
function getExePath ( regVal )
' remove any stuff that we don't want in the path
regVal = replace ( regVal, """", "" )
regVal = replace ( regVal, "@", "" )
regVal = replace ( regVal, "?", "" )
regVal = replace ( regVal, "file:///", "" )
regVal = replace ( regVal, "resource=", "" )
' expand any env settings
iFirst = instr ( 1, regVal, "%" )
iSecond = instr ( iFirst + 1, regVal, "%" )
if iFirst <> 0 and iSecond <> 0 then
envString = mid ( regVal, iFirst, iSecond )
expandedString = expandEnvString ( envString )
regVal = replace ( regVal, envString, expandedString )
end if
' not get rid of any command line options leaving just the
' path to the exe
iLoc1 = instr ( 1, lcase ( regVal), ".exe" )
iLoc2 = instr ( 1, lcase ( regVal), ".dll" )
if iLoc1 <> 0 then
regVal = mid ( regVal, 1, iLoc1 + 3 )
end if
if iLoc2 <> 0 then
regVal = mid ( regVal, 1, iLoc2 + 3 )
end if
getExePath = regVal
end function
'====================================================================
'== function expandEnvString - function to expand any environment
'== variables
'====================================================================
function expandEnvString ( envVal )
if isnull ( envVal ) or envVal = "" then
expandEnvString = ""
exit function
end if
expandEnvString = wshShell.expandenvironmentstrings ( envVal )
end function
Copyright © 2010 Dave Moats. All rights reserved. Links: Copyright © by their respective owners.
NO WARRANTIES EXTENDED. Void where prohibited by law. Please report any issues or broken links.
You may link to this site freely from your own site. You may quote from this site, but please include a link to the original source on the originating site.