Solved

Change Value in Registry not working

Posted on 2011-03-05
13
643 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now