Link to home
Start Free TrialLog in
Avatar of vb script
vb script

asked on

find registry value with vbscript

Hi

i want to write a script that search for the value OptionsFile and and the value data would be C:\Program Files\Tivoli\TSM\tdpsql\dsm.opt and its under the key Parameters
the problem is that i don't known the right key because its may be different name for every server - i know that the key in under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

i attach print script to this question the key Tsm Sql Scheduler could be diffrent name for every server
the Parameters key is permanent

if the   OptionsFile value is write i need to replace  it


Thx
ASKER CERTIFIED SOLUTION
Avatar of Kanti Prasad
Kanti Prasad

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vb script
vb script

ASKER

hi

i cant see this script

can you copy paste to here please ?
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If
 
' Duplicate these to account for each abbreviation
Const HKCR = &H80000000
Const HKEY_CLASSES_ROOT = &H80000000
Const HKCU = &H80000001
Const HKEY_CURRENT_USER = &H80000001
Const HKLM = &H80000002
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKUSERS = &H80000003
Const HKEY_USERS = &H80000003
Const HKCC = &H80000005
Const HKEY_CURRENT_CONFIG = &H80000005
 
'Value types
Const REG_SZ = 1
Const REG_EXPAND_SZ = 2
Const REG_BINARY = 3
Const REG_DWORD = 4
Const REG_MULTI_SZ = 7
 
strComputer = "."
 
Set objRegistry=GetObject("winmgmts:\\" & _ 
    strComputer & "\root\default:StdRegProv")
 
strKeyPath = "HKCU\Software"
If Right(strKeyPath, 1) = "\" Then strKeyPath = Left(strKeyPath, Len(strKeyPath) - 1)
strRoot = UCase(Left(strKeyPath, InStr(strKeyPath, "\") - 1))
strKeyPath = Mid(strKeyPath, InStr(strKeyPath, "\") + 1)
 
arrChanges = Array(_
	"TestValueHere--|--TestValueChanged",_
	"TestValue2--|--TestValue2Changed"_
	)
 
For Each strChange In arrChanges
	strValueToFind = Split(strChange, "--|--")(0)
	
	strReplaceWith = Split(strChange, "--|--")(1)
	
	Select Case strRoot
		Case "HKCR", "HKEY_CLASSES_ROOT"
			strRootKey = HKCR
		Case "HKCU", "HKEY_CURRENT_USER"
			strRootKey = HKCU
		Case "HKLM", "HKEY_LOCAL_MACHINE"
			strRootKey = HKLM
		Case "HKUSERS", "HKEY_USERS"
			strRootKey = HKUSERS
		Case "HKCC", "HKEY_CURRENT_CONFIG"
			strRootKey = HKCC
		Case Else
			MsgBox "Invalid root key entered."
			WScript.Quit
	End Select
	
	strFoundAt = ""
	
	SearchKeys strRootKey, strKeyPath, strValueToFind, strReplaceWith
	
	WScript.Echo "Finished searching subkeys."
	
Next
 
Sub ChangeValue(strRootPath, strPath, strType, strReplacement)
	Wscript.Echo strValueToFind & " was found at" & VbCrLf & _
		strPath & VbCrLf & _
		"Value Type: " & strType & VbCrLf
	strKey = Left(strPath, InStrRev(strPath, "\") - 1)
	strValue = Mid(strPath, InStrRev(strPath, "\") + 1)
	Select Case strType
		Case "String"
			On Error Resume Next
			intReturn = objRegistry.SetStringValue(strRootPath, strKey, strValue, strReplacement)
			Err.Clear
			On Error GoTo 0
		Case "ExpandedString"
			On Error Resume Next
			intReturn = objRegistry.SetExpandedStringValue(strRootPath, strKey, strValue, strReplacement)
			Err.Clear
			On Error GoTo 0
		Case "Binary"
			On Error Resume Next
			intReturn = objRegistry.SetBinaryValue(strRootPath, strKey, strValue, strReplacement)
			Err.Clear
			On Error GoTo 0
		Case "DWord"
			On Error Resume Next
			intReturn = objRegistry.SetDWORDValue(strRootPath, strKey, strValue, strReplacement)
			Err.Clear
			On Error GoTo 0
		Case "MultiString"
			On Error Resume Next
			intReturn = objRegistry.SetMultiStringValue(strRootPath, strKey, strValue, Array(strReplacement))
			Err.Clear
			On Error GoTo 0
	End Select
	If intReturn = 0 Then
		WScript.Echo "Changed from " & strValueToFind & " to " & strReplaceWith & VbCrLf
	Else
		WScript.Echo "Failed to change from " & strValueToFind & " to " & strReplaceWith
	End If
End Sub
 
Sub SearchKeys(strRootPath, strPath, strFind, strReplaceWith)
	'strFoundAt = ""
	SearchValues strRootPath, strPath, strFind, strReplaceWith
	objRegistry.EnumKey strRootPath, strPath, arrSubkeys
	If TypeName(arrSubkeys) <> "Null" Then
		For Each objSubkey In arrSubkeys
			'WScript.Echo strPath & "\" & objSubKey
			SearchKeys strRootPath, strPath & "\" & objSubKey, strFind, strReplaceWith
		Next
	End If
End Sub
 
Sub SearchValues(strRootPath, strPath, strFind, strReplaceWith)
	strType = ""
	objRegistry.EnumValues strRootPath, strPath, arrValueNames, arrValueTypes
	If TypeName(arrValueNames) <> "Null" Then
		For intVal = LBound(arrValueNames) To UBound(arrValueNames)
		    Select Case arrValueTypes(intVal)
				Case REG_SZ
					If VarType(strFind) = vbString Then
						objRegistry.GetStringValue strRootPath, strPath, arrValueNames(intVal), strValue
						If strValue = strFind Then
							strFoundAt = strPath & "\" & arrValueNames(intVal)
							strType = "String"
						End If
					End If
				Case REG_EXPAND_SZ
					If VarType(strFind) = vbString Then
						objRegistry.GetExpandedStringValue strRootPath, strPath, arrValueNames(intVal), strValue
						If strValue = strFind Then
							strFoundAt = strPath & "\" & arrValueNames(intVal)
							strType = "ExpandedString"
						End If
					End If
				Case REG_BINARY
					If VarType(strFind) = vbByte Then
						objRegistry.GetBinaryValue strRootPath, strPath, arrValueNames(intVal), strValue
						If strValue = strFind Then
							strFoundAt = strPath & "\" & arrValueNames(intVal)
							strType = "Binary"
						End If
					End If
				Case REG_DWORD
					If VarType(strFind) = vbString Then
						objRegistry.GetDWordValue strRootPath, strPath, arrValueNames(intVal), strValue
						If strValue = strFind Then
							strFoundAt = strPath & "\" & arrValueNames(intVal)
							strType = "DWord"
						End If
					End If
				Case REG_MULTI_SZ
					If VarType(strFind) = vbString Then
						objRegistry.GetMultiStringValue strRootPath, strPath, arrValueNames(intVal), arrValues
						For Each strValue In arrValues
							If strValue = strFind Then
								strFoundAt = strPath & "\" & arrValueNames(intVal)
								strType = "MultiString"
							End If
						Next
					End If
			End Select
			If strFoundAt <> "" Then
				ChangeValue strRootPath, strFoundAt, strType, strReplaceWith
				strFoundAt = ""
			End If
		Next
	End If
End Sub

Open in new window

Avatar of Frank Helk
How about that:

Dim winShl
Dim regVal
Set winShl = CreateObject("WScript.Shell")
regVal = winShl.RegRead("someRegKey")

Open in new window

hi frankhelk

this what i need :
Dim winShl
Dim regVal
Set winShl = CreateObject("WScript.Shell")
regVal = winShl.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\adsi\Cache\PerMachine")
wscript.echo regVal

the cache is the registry i don't know exactly his name

i need something like this

Dim winShl
Dim regVal
Set winShl = CreateObject("WScript.Shell")
regVal = winShl.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\adsi\.\PerMachine")
wscript.echo regVal
OK - the solution I gave you reads a value with an exactly known registry path.

If you don't know the eact location, you'll have to iterate thru the respective registry key ... there are several ways to do that, and I don't want to post other people's code.

If you squeeze a serch engine for "WScript.Shell parse registry vbs" you should get a load of examples ...
hi
i found this code
Const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"

Sub EnumerateKeys(hive, key)
  WScript.Echo key
  reg.EnumKey hive, key, arrSubKeys
  If Not IsNull(arrSubKeys) Then
    For Each subkey In arrSubKeys
      EnumerateKeys hive, key & "\" & subkey
    Next
  End If
End Sub

Set reg = GetObject("winmgmts://./root/default:StdRegProv")

EnumerateKeys HKEY_LOCAL_MACHINE, strKeyPath

but this search for key , i need to search inside the key like

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Adobe Flash Player ActiveX\Helplink

this script search in uninstall key for subkey and i need to search key & subkey & Helplink ( value )
That script uses recusion to enumerate all keys within a given key, but it does nothing with that.

Besides of calling EnumerateKeys () again to dive deeper into the subkeys, it should enumerate the values, too with reg.EnumValues and i.e. add their paths to a list of values (probably with pre-filtering them). After parsing everything in the subkey you could use the list to find the values key(s) you're really interested in.

P.S.: In the  aspect of that post, a "key" is like a directory on a disk, a "subkey" is like a subdirectory, and a "value" is like a file on the disk, which has some content ... a somewhat irritating naming convention from Microsoft ....
i want to write a script that search for the value OptionsFile and and the value data would be C:\Program Files\Tivoli\TSM\tdpsql\dsm.opt and its under the key Parameters
the problem is that i don't known the right key because its may be different name for every server - i know that the key in under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

i attach print script to this question the key Tsm Sql Scheduler could be diffrent name for every server
the Parameters key is permanent

if the   OptionsFile value is write i need to replace  it


Thx

can you help with this please ? this is excatly what i need ?
I've requested that this question be closed as follows:

Accepted answer: 0 points for vb script's comment #a40880902

for the following reason:

good
good