Avatar of vb script
vb script
 asked on

Search for registry key

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
VB ScriptVisual Basic ClassicScripting Languages

Avatar of undefined
Last Comment
vb script

8/22/2022 - Mon
Bill Prew

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

~bp
Bill Prew

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
vb script

ASKER
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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Bill Prew

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
vb script

ASKER
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
Frank Helk

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
vb script

ASKER
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
Frank Helk

Hmmm - could you please give some examples how that part changes ? Making such registry key names kind of random seems strange to me ....
vb script

ASKER
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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Bill Prew

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
vb script

ASKER
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 Prew

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Frank Helk

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

ASKER
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
vb script

ASKER
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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
vb script

ASKER
wondefull job