Search for registry key

vb script
vb script used Ask the Experts™
on
hi to every body
i want to write a vb script that will search for key - the key is under - HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\

the name of the key will start with "TSM"

Thx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Is it always right in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\, or could it be nested in subkeys below that?

~bp
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Give this a try.

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strMatch = "TSM"
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
    strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SYSTEM\CurrentControlSet\Services"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
 
intCount = 0 
For Each subkey In arrSubKeys
   If UCase(Left(subkey, Len(strMatch))) = UCase(strMatch) Then
      Wscript.Echo subkey
      intCount = intCount + 1
   End If
Next

If intCount = 0 Then
   Wscript.Echo "No keys found starting with """ & strMatch & """."
End If

Open in new window

~bp

Author

Commented:
Hi

your script work like charm , but i forgot something , What I want to write actually is such a thing
If the resulting value is equal to TSM so I want to check the following key - strKeyPath & "\" & subkey & "\ Parameters"
And below this key has a value named (REG_SZ) named OptionsFile and its value is equal to C: \ Program Files \ tivoli \ tsm \ baclient \ dsm.opt If all the conditions are right I want to change two values that are under strKeyPath & "\" & subkey & "\ Parameters "

Thanks in advance
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Can you export that part of the registry from an example system and post the REG file here so i can test?  And include details on what values need to be changed.

~bp

Author

Commented:
Hi

here is the registry key
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\TSM Client Scheduler\Parameters]
"OptionsFile"="C:\\Program Files\\Tivoli\\TSM\\baclient\\dsm.opt"
"ScheduleLog"="C:\\Program Files\\Tivoli\\TSM\\baclient\\dsmsched.log"
"ErrorLog"="C:\\Program Files\\Tivoli\\TSM\\baclient\\dsmerror.log"

The first condition is "OptionsFile"="C:\\Program Files\\Tivoli\\TSM\\baclient\\dsm.opt"

and if true i will change both value ScheduleLog And ErrorLog

Thx
Just meditating ... in my opinion there's no need to search for that special key by traversing thru the [HKLM\SYSTEM\CurrentControlSet\services] branch of the registry tree.

Just try to read the [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\TSM Client Scheduler\Parameters\OptionsFile] value. Use a proper On Error statement to react on failure.

If the attept to read the value fails, it' supposedly simply not there - that results in an error and you could react on that where the On Error statement points to. Otherwise you get the value and could check that for the specific value.

That way you don't have to scrub the entire branch just to detect if the subkey is there.

Author

Commented:
hi

the name - TSM Client Scheduler is different on each server this why i search the word TSM
can you give an exmple please how to write this script
Hmmm - could you please give some examples how that part changes ? Making such registry key names kind of random seems strange to me ....

Author

Commented:
o.k
I will explain - the reason I'm looking for the key that starts with TSM reason may be more than one key that starts with TSM, movie importers renamed I want to check out the next OptionsFile which is equivalent to C: \ Program Files \ Tivoli \ TSM \ baclient \ dsm.opt and if all conditions There I met the two following values ScheduleLog and ErrorLog these values:
C: \ Program Files \ Tivoli \ TSM \ baclient \ ComputerName_sched.log
C: \ Program Files \ Tivoli \ TSM \ baclient \ ComputerName_error.log

If its possible here its a vbs that search for the Key that start with TSM - the problem is that i dont know how to complete the rest of the think.

Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strMatch = "TSM"
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SYSTEM\CurrentControlSet\Services"
oReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
 
intCount = 0
For Each subkey In arrSubKeys
   If UCase(Left(subkey, Len(strMatch))) = UCase(strMatch) Then
      Wscript.Echo subkey
      intCount = intCount + 1
   End If
Next

If intCount = 0 Then
   Wscript.Echo "No keys found starting with """ & strMatch & """."
End If


Thanks in advance
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Okay, this should do what you are describing, you just have to add whatever logic is needed to change the existing values to the new desired values.

Keep in mind this will need to be run with elevated privs in order to update the registry.

' Constant values
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strMatch = "TSM"

' Create register opject
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
 
' Access the root key we want to search 
strKeyPath = "SYSTEM\CurrentControlSet\Services"
objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
 
' Look for the desired key 
For Each strSubKey In arrSubKeys

   If UCase(Left(strSubKey, Len(strMatch))) = UCase(strMatch) Then
      ' When found process this key
      ProcessKey strKeyPath & "\" & strSubKey & "\Parameters"
   End If

Next

Sub ProcessKey(strKey)

   ' Look for "OptionsFile" value
   objReg.GetStringValue HKEY_LOCAL_MACHINE, strKey, "OptionsFile", strValue

   ' If found then update desired values
   If Not IsNull(strValue) Then

      ' Get current value of string
      objReg.GetStringValue HKEY_LOCAL_MACHINE, strKey, "ScheduleLog", strValue
      If Not IsNull(strValue) Then
         ' Adjust strValue as needed here, and then update
         strValue = Replace(strValue, "C:", "X:")
         objReg.SetStringValue HKEY_LOCAL_MACHINE, strKey, "ScheduleLog", strValue
      End If

      ' Get current value of string
      objReg.GetStringValue HKEY_LOCAL_MACHINE, strKey, "ErrorLog", strValue
      If Not IsNull(strValue) Then
         ' Adjust strValue as needed here, and then update
         strValue = Replace(strValue, "C:", "X:")
         objReg.SetStringValue HKEY_LOCAL_MACHINE, strKey, "ErrorLog", strValue
      End If

   End If

End Sub

Open in new window

~bp

Author

Commented:
hi

To change the value I need the value of OptionsFile will be equal to C: \ Program Files \ tivoli \ tsm \ baclient \ dsm.opt because in some cases it might be worth a different value
Where I make it in the script
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
These lines (in two places):

         ' Adjust strValue as needed here, and then update
         strValue = Replace(strValue, "C:", "X:")


I gave you an example that changed the drive letter, but you can replace the whole string value with the desired one if that's your need.

~bp
OK - if a part of the registry path is uncertain, you'll definitely have to parse thru them.

Author

Commented:
I checked the script but I want him to do - if the value of OptionsFile will be equal to C: \Program Files \tivoli\tsm\baclient\dsm.opt only then is there a need to change the following values ScheduleLog  & ErrorLog to C:\ProgramFiles\tivoli\tsm\baclient\ComputerName_sch.log
C:\Program Files\tivoli\tsm\baclient\ComputerName_error.log

What that does is currently Script changing them regardless of the value OptionsFile

Is there a solution

Author

Commented:
hi again

i want to write this value C:\ProgramFiles\tivoli\tsm\baclient\ComputerName_sch.log to ScheduleLog
and C:\Program Files\tivoli\tsm\baclient\ComputerName_error.log to ErrorLog
not replace
IT / Software Engineering Consultant
Top Expert 2016
Commented:
Okay, this should do what you described.

' Constant values
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strMatch = "TSM"

' Create register opject
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
 
' Access the root key we want to search 
strKeyPath = "SYSTEM\CurrentControlSet\Services"
objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
 
' Look for the desired key 
For Each strSubKey In arrSubKeys

   If UCase(Left(strSubKey, Len(strMatch))) = UCase(strMatch) Then
      ' When found process this key
      ProcessKey strKeyPath & "\" & strSubKey & "\Parameters"
   End If

Next

Sub ProcessKey(strKey)

   ' Look for "OptionsFile" value
   objReg.GetStringValue HKEY_LOCAL_MACHINE, strKey, "OptionsFile", strValue

   ' If found then update desired values
   If LCase(strValue) = LCase("C:\\Program Files\\tivoli\\tsm\\baclient\\dsm.opt") Then

      ' Get current value of string
      objReg.GetStringValue HKEY_LOCAL_MACHINE, strKey, "ScheduleLog", strValue
      If Not IsNull(strValue) Then
         strValue = "C:\\Program Files\\tivoli\\tsm\\baclient\\ComputerName_sch.log"
         objReg.SetStringValue HKEY_LOCAL_MACHINE, strKey, "ScheduleLog", strValue
      End If

      ' Get current value of string
      objReg.GetStringValue HKEY_LOCAL_MACHINE, strKey, "ErrorLog", strValue
      If Not IsNull(strValue) Then
         strValue = "C:\\Program Files\\tivoli\\tsm\\baclient\\ComputerName_error.log"
         objReg.SetStringValue HKEY_LOCAL_MACHINE, strKey, "ErrorLog", strValue
      End If

   End If

End Sub

Open in new window

~bp

Author

Commented:
wondefull job

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial