Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Registry query via variable string and writing a new reg key

Status
Not open for further replies.

rathgus

IS-IT--Management
Oct 19, 2010
6
US
I apologize in advance for what I'm about to ask. Our dedicated scripter in the office just got laid off and somehow I got tasked with a project involving VBScript, of which I know nothing about.

I'll provide screenshots of what I want to ultimately do, to try to help with the verbiage.

Generic Goal: I'm trying to make a script that will query the registry for a specific value under a specific string. If that value exists, I need to add new reg value under that string.

Reason: When Blackberry Device Manager is installed, it automatically creates a new modem called "Standard Modem". In order to get an AT&T Blackberry to tether to a laptop, a string has to be entered under the Standard Modem's properties; as shown here:
In the Registry, the modems appear here: On the left side, you see the "0000" subkey. If there are multiple modems installed, be it multiple aircards or whatnot, there will be multiple subkeys; "0000", "0001", "0002", "0003", etc. for however modems are installed. Since there is only 1 modem installed on the machine I screenshotted, it only shows "0000". So if there are 4 modems installed on the machine prior to installing the Blackberry Device Manager... after it is installed "Standard Modem" will be the "0004" subkey.

What I'm trying to do, is create a script that will query each registry subkey until it finds "Standard Modem" by matching the "FriendlyName". Once it finds "Standard Modem" it will write a new reg value to that subkey; as shown here: See the "UserInit" value?

This is the code I have so far; but like I said, I'm not sure if all those arguments are valid or if the syntax is correct.

Code:
'Variable Declaration
Option Explicit
Dim oReg, strKeyPath1, strValueName1, strValue1
Dim oReg, strKeyPath2, strValueName2, strValue2

Const HKCU = &H80000001
Const HKLM = &H80000002

'Suppresses Errors
On Error Resume Next

'Sets up connection to registry
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\root\default:StdRegProv")

set strSubKey1 = "0000"
set strKeyPath1 = "SYSTEM\ControlSet001\Control\Class\{4D36E96D-E325-11CE-BFC1-08002BE10318}\" 
set strValueName1 = "FriendlyName"

'Start of script
oReg.GetStringValue HKLM, strKeyPath1 & strSubKey1, strValueName1, strValue1
  While strValue1 <> "Standard Modem"
    strSubKey1 = strSubKey1 + 1
	oReg.GetStringValue HKLM, strKeyPath1 & strSubKey1, strValueName1, strValue1
  Wend
    oReg.SetStringValue HKLM, strKeyPath1 & strSubKey1, "UserInit", "AT+CGDCONT=2,\"IP\",\"wap.cingular\""
'EOF



When I run it though, it just sits at the command prompt. I assume it's just endless looping. I took some C++ courses in College, so I get the jist of what I'm looking at, even though its another language. Any advice? Thanks!
 
In your while loop you have
Code:
strSubKey1 = strSubKey1 + 1
This will convert 'strSubKey1' to a numeric value and add the results together. "0000" converts to 0, so strSubKey1 is taking on values 1, 2, 3, 4, 5,... instead of 0001, 0002, 0003, ...
 
have an integer which you increment, then do some sort of string format by strTemp = "000" & CStr(intSubKey). the example isnt a good one as it only handles up to 9 but you can work on that

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Like this ?
strSubKey1 = Right("000" & CStr(strSubKey1 + 1), 4)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
exactly PHV

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Thanks for the responses guys, I'll try that change today and report back.

Other than that, does the syntax look correct to you guys?

My only other concern is trying to apply the string value "AT+CGDCONT=2,\"IP\",\"wap.cingular\"". I'm using Notepad++ to help me see the code and I don't think it likes multiple quotation marks in there. Should it write that string from the first quotation mark to the last? Or do I need to do something else?

Thanks again!
 
i dont think your use of the " in that funny string are correct, or at least the SetStringValue method call will fail

but, seeing as you have

'Suppresses Errors
On Error Resume Next

i guess you arent concerned?

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Replace this:
"AT+CGDCONT=2,\"IP\",\"wap.cingular\""
with this:
"AT+CGDCONT=2,'"IP'",'"wap.cingular"""

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV, I replaced
Code:
strSubKey1 = strSubKey1 + 1
with
Code:
strSubKey1 = Right("000" & CStr(strSubKey1 + 1), 4)
like you suggested, but it still seems to be stuck in loop. I even replaced the funky string I want to add with "test" to see if it would do that, and nothing, still looping.

Do I need to change the set strSubKey1 value to something different besides 0000? I don't think so because if I did that, it wouldn't read the reg key at all the first time.

Anything else I can try? Thanks!
 
Is it possible the SetStringValue method I'm using won't work? Since the reg string I want to add does not yet exist, would I have to create the reg string, then set it?

Thanks.
 
im not surprised your script is stuck in a loop, it will loop forever if 'Standard Modem' is not returned. looping in scripts need to be handled with care.

the enumkeys method of WMI is about the best registry based method WMI offers. it is great

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Thanks for all your help guys, I really appreciate it. I checked out the links and started to do more research on loops. I re-did the script and approached with a Do loop:

Code:
const HKLM = &H80000002 
strComputer = "."

Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "SYSTEM\ControlSet001\Control\Class\{4D36E96D-E325-11CE-BFC1-08002BE10318}\"
strSubKey = "0000"

Do Until strValue = "Standard Modem"
	oReg.GetStringValue HKLM, strKeyPath & strSubKey, "FriendlyName", strValue
	WScript.Echo strValue
	WScript.Echo strKeyPath & strSubKey
	strSubKey = Right("000" & CStr(strSubKey + 1), 4) 	
Loop
oReg.SetStringValue HKLM, strKeyPath & strSubKey, "UserInit", "test"

As you can see, to test it, I put in some Echo statements. The loop performs as it should (without infinite looping!), going through each subkey and reporting the appropriate value (as shown here The only thing now though, is the code after the Loop isn't executing at all. No values are being written to the registry. Do I have to return the strSubKey value from the loop somehow?

Thanks!
 
Ahhh... it seems the strSubKey is getting incremented one last time before it leaves the loop. So the value is one higher than I want it. To fix the issue I added:

strSubKey = Right("000" & CStr(strSubKey - 1), 4)

After Loop and before oReg.SetStringValue HKLM, strKeyPath & strSubKey, "UserInit", "test".

It's probably not best coding practice to do it that way, but it works! Thank you guys for all your support!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top