Solved

Change Value in Registry not working

Posted on 2011-03-05
13
646 Views
Last Modified: 2012-05-11
I have a script that searches a registry for a value and if its 0 change it to a 1.  I can find the key but it will not write the value.  
Const HKEY_LOCAL_MACHINE = &H80000002 
strComputer = "." 
Set oReg=GetObject("winmgmts:\\" &_ 
  strComputer & "\root\default:StdRegProv")

For x=0 to 9 

strKeyPath = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\000"& x 
strValueName = "LOM" 
oReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue 
If strValue="0" then 
 oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,1 
 End If 

Next 

For x=10 to 99 

strKeyPath = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\00"& x 
strValueName = "LOM" 
oReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue 
If strValue="0" Then 
  oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,1 
  End If 

Next

Open in new window

0
Comment
Question by:rschraeger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 35044664
0
 

Author Comment

by:rschraeger
ID: 35044956
I tired that script but it does not work.  The syntax is wrong.  I am acutal looking for someone to help me with my script rather then suggest a different script.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 35045015
Saturdays are some times slow.  If no one else helps, click on "Request Attention" above to get others to look at your question.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:prashanthd
ID: 35046890
Hi,

values you are trying to set are string only?
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35046912
You can try the following
Const HKEY_LOCAL_MACHINE = &H80000002 
strComputer = "." 
Set oReg=GetObject("winmgmts:\\" &_ 
strComputer & "\root\default:StdRegProv")

For x=0 To 9 
    
    strKeyPath = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\000"& x 
    strValueName = "LOM" 
    oReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue 
    If strValue="0" Then
        strvalue1="1" 
        oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strvalue1
    End If 
    
Next 

For x=10 To 99 
    
    strKeyPath = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\00"& x 
    strValueName = "LOM" 
    oReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue 
    If strValue="0" Then 
        strvalue1="1"
        oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strvalue1
    End If 
Next

Open in new window

0
 

Author Comment

by:rschraeger
ID: 35055696
still did not work
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35055909
can you give the following inside the if <condition>, to check if condition is true for any value

wscript.echo strvalue
0
 

Author Comment

by:rschraeger
ID: 35055921
I did the echo's before and the condition comes back correctly.
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35056075
You are using the following

To get value - oReg.GetExpandedStringValue

To set Value - oReg.SetStringValue

Try using oReg.SetExpandedStringValue for setting the value
0
 

Author Comment

by:rschraeger
ID: 35057341
No change after setting the  oReg.SetExpandedStringValue
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 35060035
hmmm...I have tested the code you have posted and works perfectly...without any issue..

No errors are returned? Do you have the required privileges?
0
 

Author Comment

by:rschraeger
ID: 35063303
I get a complete error code too but the setting is not changed in my registry.  I am running it with admin privledges.  I am running this under a Windows 7 client.
0
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 total points
ID: 35065307
Windows 7 you cannot modify the registry without elevation.

Right click and hit 'run as administrator' on command prompt

Even if you are logged in with admin rights you still need to elevate in Windows 7.

Also modified the code for display error number and description to troubleshoot further
on error resume next

Const HKEY_LOCAL_MACHINE = &H80000002 
strComputer = "." 
Set oReg=GetObject("winmgmts:\\" &_ 
  strComputer & "\root\default:StdRegProv")

For x=0 to 9 

strKeyPath = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\000"& x 
strValueName = "LOM" 
oReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue 
If strValue="0" Then
Err.Clear 
 oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,1 
 If Err.Number<> 0 Then
 	WScript.Echo Err.Number & " - " & Err.Description
 End If
 
End If 

Next 

For x=10 to 99 

strKeyPath = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\00"& x 
strValueName = "LOM" 
oReg.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue 
If strValue="0" Then 
  oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,1 
  End If 

Next

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question