Why am I getting a permissions error when adding registry values through VBScript?

0

I've been trying to run a VBScript file every time my system starts up, and have tried modifying the registry using VBScript to add it to the list of programs to run, but I'm facing a problem with permissions even though I'm the computers administrator. I'm trying to add VB_Start.vbs to the list of startup tasks by modifying the registry with the following VBScript code:

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\Visual Basic" , "VB_Start.vbs" , "REG_SZ"
Set WshShell = Nothing

When I execute this code I'm getting the following error:

Error: Invalid root in registry key HKEY_LOCAL_MACHINE\SOFTWARE\......
Error Code: 80070005
Source: WshShell.RegWrite

user226438

Posted 2013-07-10T18:43:53.437

Reputation: 21

Answers

1

80070005 indicates an access denied error. Just because you're an admin doesn't mean your VBS is being executed automatically with admin rights. Add the following code to the top of the script:

If WScript.Arguments.Named.Exists("elevated") = False Then
  CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
  WScript.Quit
End If

The code will detect if the script is running elevated, else relaunch itself with admin rights (you'll still see the prompt of course).

Karan

Posted 2013-07-10T18:43:53.437

Reputation: 51 857

0

Const HKEY_CURRENT_USER = &H80000001

strComputer = "."

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

strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
strValueName = "Hidden"
strStringValues = "0"

oReg.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,strStringValues

This is the format of code I use for adding registry keys. All you'd have to change is what's in strValueName, the strStringValues, and the initial CONST to point to the proper section in the registry. Those codes are:

Const HKEY_CLASSES_ROOT     = &H80000000
Const HKEY_CURRENT_USER     = &H80000001
Const HKEY_LOCAL_MACHINE    = &H80000002
Const HKEY_USERS        = &H80000003
Const HKEY_CURRENT_CONFIG   = &H80000005

Kruug

Posted 2013-07-10T18:43:53.437

Reputation: 5 078